1

我不知道如何将我在 c# 上编写的代码与 FileSystemWatcher 类集成

    public static void watcherFunc()
    {
        FileSystemWatcher fileWatcher = new FileSystemWatcher(@"C:\Documents and Settings\Develop\Desktop\test\");
        fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
        fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
        fileWatcher.EnableRaisingEvents = true;
    }
    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
    }

我试图在 form1 领先活动中调用它......我试图阅读如何做到这一点并在没有运气的情况下用谷歌搜索它请帮助......谢谢!

4

1 回答 1

2

据我所知,问题是FileSystemWatcher当您完成该方法时您超出了范围。因此,您不再需要捕获事件。

像这样尝试:

FileSystemWatcher fileWatcher = new FileSystemWatcher(@"C:\Documents and ettings\Develop\Desktop\test\");
public void Initialize() //initialization or Constructor
{
    fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
    fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
    fileWatcher.EnableRaisingEvents = true;
}

// Define the event handlers. 
private void OnChanged(object source, FileSystemEventArgs e)
{
    // Specify what is done when a file is changed, created, or deleted.
    MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
}

如需更多帮助,请参阅FileWatcher 教程

于 2013-07-29T20:54:45.830 回答