Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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
C# 在FileWatcher中更改事件时引发_C# - Fatal编程技术网

C# 在FileWatcher中更改事件时引发

C# 在FileWatcher中更改事件时引发,c#,C#,我正在使用FileWatcher监视一个xml文件以跟踪更改。我只想在文件内容发生更改、文件重新命名甚至删除时触发一些方法 订阅Changed事件就足够了吗 我还需要订阅其他事件吗?为了监视您想要的所有操作,您必须侦听所有事件:创建、更改、删除、更新 以下是示例: public void init() { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = "path/to/file";

我正在使用
FileWatcher
监视一个xml文件以跟踪更改。我只想在文件内容发生更改、文件重新命名甚至删除时触发一些方法

订阅
Changed
事件就足够了吗


我还需要订阅其他事件吗?

为了监视您想要的所有操作,您必须侦听所有事件:创建、更改、删除、更新

以下是示例:

public void init() {

    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = "path/to/file";

    watcher.NotifyFilter = NotifyFilters.LastAccess
            | NotifyFilters.LastWrite | NotifyFilters.FileName
            | NotifyFilters.DirectoryName;
    // Only watch text files.
    watcher.Filter = "*.txt";

    // Add event handlers.
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Deleted += new FileSystemEventHandler(OnChanged);
    watcher.Renamed += new RenamedEventHandler(OnRenamed);

    // Begin watching.
    watcher.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.        
}

private static void OnRenamed(object source, RenamedEventArgs e) {
    // Specify what is done when a file is renamed.     
}