2011-05-19 78 views
2

目前我正在試圖排除與FileSystemWatcher類目錄,雖然我已經使用這個:FileSystemWatcher類 - 排除目錄

FileWatcher.Filter = "C:\\$Recycle.Bin"; 

FileWatcher.Filter = "$Recycle.Bin"; 

它編譯好的,但沒有結果顯示當我嘗試這個。

如果我參加了過濾掉,所有的文件加載罰款,代碼如下:

static void Main(string[] args) 
     { 
      string DirPath = "C:\\"; 

      FileSystemWatcher FileWatcher = new FileSystemWatcher(DirPath); 
      FileWatcher.IncludeSubdirectories = true; 
      FileWatcher.Filter = "*.exe"; 
      // FileWatcher.Filter = "C:\\$Recycle.Bin"; 
      // FileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed); 
      FileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created); 
      // FileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted); 
      // FileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed); 
      FileWatcher.EnableRaisingEvents = true; 

      Console.ReadKey(); 
     } 

回答

7

你可能都沒有看過http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter.aspx。您不能使用Filter屬性排除任何內容。它只包含匹配過濾器的對象。

如果您想排除某些內容,請在由FSW發起的事件中執行此操作。

+0

瘋狂的事情,actualy我,我只是想知道如果有一個備用根,感謝您的幫助,雖然! :) – Ken 2011-05-19 22:24:15

0

確定該文件是在你的事件處理程序的目錄,什麼事都不做,那麼:

private void WatcherOnCreated(object sender, FileSystemEventArgs fileSystemEventArgs) 
{ 
    if (File.GetAttributes(fileSystemEventArgs.FullPath).HasFlag(FileAttributes.Directory)) 
     return; //ignore directories, only process files 

    //TODO: Your code handling files... 
}