如何在c#上跟踪指定文件夹中的更改

时间:2014-02-06 20:23:25

标签: c#

  1. 我需要以下内容来添加和删除文件和文件夹。
  2. 我需要关注文件和文件夹的更改。
  3. 所有操作都必须递归。
  4. 如何在c#上完成所有操作?

1 个答案:

答案 0 :(得分:0)

使用

 FileSystemWatcher 
这是一个简单的代码示例,用于查看已创建的文件:

public void Watcher()
{
    //Create a new FileSystemWatcher.
    FileSystemWatcher watcher = new FileSystemWatcher();

    //Set the filter to only catch TXT files.
    watcher.Filter = "*.txt";

    //Subscribe to the Created event.
    //Created Occurs when a file or directory in the specified Path is created.
    //You can change this based on what you are trying to do.
    watcher.Created += new FileSystemEventHandler(YOUR_EVENT_HANDLER);

    //Set the path to you want to monitor
    watcher.Path = @"C:\PATH\";

    //Enable the FileSystemWatcher events.
    watcher.EnableRaisingEvents = true;
}