0

Hi im programming a windows service that will monitor changes in an specified folders, im logging to the event viewer with several try-catchs but when finish all the code specified in the onStart, throws the exception object not set to an instance... I will really appreciate qny advice because im turning around this problem and didnt found nothing in google...

this is my code:

    protected override void OnStart(string[] args)
    {
        CreateWatchers();
    }

public void CreateWatchers()
    {
        try
        {
            Carpeta[] carpetas = new Carpeta[100];
            carpetas[0] = new Carpeta { IdCarpeta = 1, FullPath = @"C:\VASSALLO\BELGRANO\RM\" };
            carpetas[1] = new Carpeta { IdCarpeta = 2, FullPath = @"C:\VASSALLO\OLIVOS\RM\" };
            for (int i = 0; i < carpetas.Length; i++)
            {
                if (!string.IsNullOrEmpty(carpetas[i].FullPath))
                {
                    Watchers w = new Watchers(carpetas[i]);
                    Lista.Add(w);
                }
            }
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(ex.Message);
        }
    }

class Watchers
{
    private string miNombre;

    public Watchers(Carpeta carpeta)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(@carpeta.FullPath)
        {
            IncludeSubdirectories = true,
            NotifyFilter = NotifyFilters.LastAccess
                         | NotifyFilters.LastWrite
                         | NotifyFilters.FileName
                         | NotifyFilters.DirectoryName
        };

        watcher.Changed += Modificacion;
        watcher.Created += Creacion;
        watcher.Deleted += Eliminacion;
        watcher.Renamed += Renombrado;
        watcher.EnableRaisingEvents = true;
        miNombre = "ServicioCarpeta" + carpeta.IdCarpeta.ToString();

    }
    static void Modificacion(object source, FileSystemEventArgs e) => Novedad(e.ChangeType.ToString(), e.FullPath);
    static void Creacion(object source, FileSystemEventArgs e) => Novedad(e.ChangeType.ToString(), e.FullPath);
    static void Eliminacion(object source, FileSystemEventArgs e) => Novedad(e.ChangeType.ToString(), e.FullPath);
    static void Renombrado(object source, RenamedEventArgs e) => Novedad(e.ChangeType.ToString(), e.FullPath, e.OldFullPath);

    private static void Novedad(string ChangeType, string fullPath, string oldFullPath = "")
    {
        File.Create("\\c:\\temp_folder\\" + ChangeType);
    }
}

0 Answers0