FileSystemWatcher在调用后重置EnableRaisingEvents

时间:2018-04-06 11:11:06

标签: c# file-watcher

我需要在创建或重命名时处理许多目录和处理文件。为此,我创建了以下Watcher类。

public class Watcher
{
    FileSystemWatcher fileSystemWatcher = null;

    public delegate void FileCreatedEventHandler(object sender, FileSystemEventArgs e);
    public delegate void FileRenamedEventHandler(object sender, RenamedEventArgs e);
    public delegate void LogEventHandler(object sender, String e);

    public event FileCreatedEventHandler FileCreated;
    public event FileRenamedEventHandler FileRenamed;
    public event LogEventHandler LogEvent;


    public String Directory { get; set; }
    public String DAP { get; set; }
    public String Filter { get; set; }
    public Watcher()
    {
        fileSystemWatcher = new FileSystemWatcher();
    }

    public Watcher(String directory, String filter, String dap)
    {
        this.DAP            = dap;
        this.Directory      = directory;
        this.Filter         = filter;
    }

    public void StartWatch()
    {
        if (fileSystemWatcher == null) fileSystemWatcher = new FileSystemWatcher();
        fileSystemWatcher.Filter = Filter;
        fileSystemWatcher.Path = Directory;
        fileSystemWatcher.EnableRaisingEvents = true;

        fileSystemWatcher.Created += FileSystemWatcher_Created;
        fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;

        Log(String.Format("Watching Directory {0}", Directory));
    }

    private void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
    {
        FileRenamed?.Invoke(sender, e);

    }

    private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
    { 
        FileCreated?.Invoke(sender, e);            
    }

    private void Log ( String Message )
    {
        LogEvent?.Invoke(this, Message);
    }

}

这又由以下函数使用,该函数设置了许多实例;每个被监视的目录都有一个。

    public ProfileWatcher()
    {
        List<nhs_acquisition_profile> p = null;
        List <Watcher> fwList = new List<Watcher>();

        InitializeComponent();

        try
        {
            if (!System.Diagnostics.EventLog.SourceExists("DirectoryScanner"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "DirectoryScanner","ProfileDirectoryScanner");
            }

            // 
            profileWatcherLog.Source = "DirectoryScanner";
            profileWatcherLog.Log = "ProfileDirectoryScanner";

            using (nhs_acquisition_profiles profiles = new nhs_acquisition_profiles(Properties.Settings.Default.DataConnection))
            {
                profileWatcherLog.WriteEntry("retrieving list of DAP Profiles");

                p = profiles.Select<nhs_acquisition_profile>(null);

                profileWatcherLog.WriteEntry(String.Format("Found {0} Directories to watch.", p.Count.ToString()));

                foreach (nhs_acquisition_profile pfl in p)
                {
                    Watcher w = null;

                    try
                    {

                        profileWatcherLog.WriteEntry(String.Format("Attempting to set-up watcher on {0} for DAP {1}",pfl.dap_file_location,pfl.dap_name));
                        w = new Watcher(pfl.dap_file_location, "*.*", pfl.dap_name);

                        profileWatcherLog.WriteEntry("Initialising Event Handlers");
                        // initialise event handlers
                        w.FileCreated   += W_FileCreated;
                        w.FileRenamed   += W_FileRenamed;
                        w.LogEvent      += W_LogEvent;
                        profileWatcherLog.WriteEntry("Event Handlers initialised");

                        w.StartWatch();

                        profileWatcherLog.WriteEntry("Watch started....Adding to Watcher List");

                        // add the watcher to the list of watchers
                        fwList.Add(w);

                        profileWatcherLog.WriteEntry("Added to list of file watchers");

                        profileWatcherLog.WriteEntry(String.Format("Watching {0} for files matching *.* for DAP {1}",pfl.dap_file_location,pfl.dap_name));
                    }
                    catch
                    {
                        throw;
                    }
                }
            }


        }
        catch (Exception e)
        {
            profileWatcherLog.WriteEntry(String.Format("ERROR: {0}",e.Message),EventLogEntryType.Error);
        }
        finally
        {
            profileWatcherLog.WriteEntry("Profile Watcher setup");
        }

    }

每次创建文件时,都应调用以下

之一
    private void W_FileRenamed(object sender, RenamedEventArgs e)
    {

        try
        {
            profileWatcherLog.WriteEntry(String.Format("File Renamed From {0} to {1}", e.OldName, e.Name));

            using (FileSystemWatcher watcher = ((System.IO.FileSystemWatcher)sender))
            {
                if (watcher != null)
                    Process(watcher.Path);
            }
        }
        catch (System.Exception ex)
        {
            profileWatcherLog.WriteEntry(ex.Message, EventLogEntryType.Error);
        }
        finally
        {

        }
    }

    private void W_FileCreated(object sender, FileSystemEventArgs e)
    {
        try
        {
            profileWatcherLog.WriteEntry(String.Format("File Created {0}", e.Name));

            using (FileSystemWatcher watcher = ((System.IO.FileSystemWatcher)sender))
            {
                if (watcher != null)
                    Process(watcher.Path);
            }
        }
        catch ( System.Exception ex)
        {
            profileWatcherLog.WriteEntry(ex.Message, EventLogEntryType.Error);   
        }
        finally
        {

        }

    }

这是第一次将文件放入目标目录时没问题。麻烦的是它从

返回后重置EnableRaisingEvents
FileCreated?.Invoke(sender, e); 

上面第一个列表中的行。

代码中的任何内容似乎都没有这样做,当我尝试在FileCreated之后重新将其重置时,然后他就会知道对象已被处理掉(即使调试器显示它有值)

然后整个事情被包裹在服务中,以便它一直运行。

0 个答案:

没有答案