2013-07-24 84 views
0

我使用下面的C#代碼來捕獲文件系統中的所有文件(如文件重命名或創建等),它的工作效果很好。但是,當我從「D:\」更改路徑到「\\ ServerName \文件夾」 - 程序停止工作。但在MSDN FileSystemWatcher類描述悲傷:「...您可以創建一個組件來觀看本地計算機上的文件,一個網絡驅動器,或一個遠程計算機 ...」 你能幫我解決我的代碼使用網絡文件夾plz。無法捕捉網絡文件更改

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace consoleWatcher 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      FileSystemWatcher myWatcher = new FileSystemWatcher("D:\\"); 
      myWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; 

      myWatcher.Changed += new FileSystemEventHandler(OnChanged); 
      myWatcher.Created += new FileSystemEventHandler(OnChanged); 
      myWatcher.Deleted += new FileSystemEventHandler(OnChanged); 
      myWatcher.Renamed += new RenamedEventHandler(OnRenamed); 

      myWatcher.IncludeSubdirectories = true; 
      myWatcher.EnableRaisingEvents = true; 

      Console.Read(); 

     } 

     private static void OnChanged(object source, FileSystemEventArgs e) 
     { 
      // Specify what is done when a file is changed, created, or deleted. 

      Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 
     } 

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

      Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); 
     } 

    } 
} 
+0

你有沒有檢查http://www.codeguru.com/csharp/.net/net_general/eventsanddelegates/article.php/c9113/FileSystemWatchermdashWhy-Does-It -Stop-Working.htm? –

+0

您是否試圖在運行時更改它? –

+0

在本地機器和遠程機器上運行什麼操作系統?我聽說(和看到)文件觀察器在Windows 8中造成問題 –

回答