2014-02-06 42 views
-6
  1. 我需要以下內容才能添加和刪除文件和文件夾。
  2. 我需要注意文件和文件夾的更改。
  3. 所有操作都必須遞歸。

如何在c#上做到這一切?如何跟蹤c上指定文件夾中的更改#

+6

任何努力這麼遠? –

+2

FileSystemWatcher –

+0

FileSystemWatcher允許所有這一切?請給我簡單的例子 – Basil

回答

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; 
} 
+0

將newFileSystemWatcher更改爲新的FileSystemWatcher – Basil

+0

@Basil感謝您的更正! :-) –