Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
FileSystemWatcher c#无错误和isn';行不通_C#_Filesystemwatcher - Fatal编程技术网

FileSystemWatcher c#无错误和isn';行不通

FileSystemWatcher c#无错误和isn';行不通,c#,filesystemwatcher,C#,Filesystemwatcher,我不知道如何将我在c#上编写的代码与FileSystemWatcher类集成 public static void watcherFunc() { FileSystemWatcher fileWatcher = new FileSystemWatcher(@"C:\Documents and Settings\Develop\Desktop\test\"); fileWatcher.NotifyFilter = NotifyFilters.Last

我不知道如何将我在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 lead event中调用它。。。。我试图阅读如何做它和谷歌它没有运气请帮助…谢谢

从我所能看出的问题是,当您完成该方法时,您的
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);
}

如果需要,请参阅以获取更多帮助

我已经有一段时间没有使用c了,但是如果您在函数中声明了一个监视程序,当这个函数完成的时候,它不是超出了范围吗?我想这就是发生的事了=)当你说它的时候,它的逻辑是正确的直到不起作用我用你的代码尝试了它+我看了教程,试着用那种方式做它,但仍然没有任何错误和结果我可以添加一个计时器也许它会起作用,但它对我不好我想要一个实时监控不是每5个min…好的,我把OnChanged事件切换到Created事件,现在它可以工作了,但我仍然不明白为什么它不能与OnChanged一起工作,我似乎无法复制您的问题。这是我用的:。当我打开一个记事本文件并将其保存在该目录中时,messagebox就会显示出来