2013-04-29 79 views
18

由於某種原因,我的FileSystemWatcher沒有發射任何事件。我想知道任何時候在我的目錄中創建,刪除或重命名新文件。 _myFolderPath正在設置正確,我已檢查。FileSystemWatcher不發射事件

這裏是我當前的代碼:

public void Setup() { 
    var fileSystemWatcher = new FileSystemWatcher(_myFolderPath); 
    fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess | 
     NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; 

    fileSystemWatcher.Changed += FileSystemWatcherChanged; 
    fileSystemWatcher.Created += FileSystemWatcherChanged; 
    fileSystemWatcher.Deleted += FileSystemWatcherChanged; 
    fileSystemWatcher.Renamed += FileSystemWatcherChanged; 

    fileSystemWatcher.Filter = "*.*"; 
    fileSystemWatcher.EnableRaisingEvents = true; 
} 

private void FileSystemWatcherChanged(object sender, FileSystemEventArgs e) 
{ 
    MessageBox.Show("Queue changed"); 
    listBoxQueuedForms.Items.Clear(); 
    foreach (var fileInfo in Directory.GetFiles(_myFolderPath, "*.*", SearchOption.TopDirectoryOnly)) 
    { 
     listBoxQueuedForms.Items.Add(fileInfo)); 
    } 
} 
+0

你做了什麼來驗證的情況發生在其中的那些事件應該火? – tnw 2013-04-29 12:37:28

+0

我已經手動創建,重命名和刪除了我的目錄中的文件。什麼都不會發生。 – gwin003 2013-04-29 12:37:50

+0

本地驅動器,其AppData /漫遊文件夾更具體 – gwin003 2013-04-29 12:39:38

回答

16

你似乎在創建FileSystemWatcher的作爲設置方法的局部變量。這種方法當然會在方法結束時超出範圍,並且很可能會在這一點上得到整理,從而消除手錶。

嘗試創建FSW的地方,它將被持續(例如一個程序級別的變量),看看是否這樣排序。

+0

我將'FileSystemWatcher'移到了一個私有局部變量,現在它似乎更加一致。 – gwin003 2013-04-29 13:25:41

+0

@ gwin003它是一個局部變量。我懷疑你的意思是你把它移到私人_field_? – 2013-04-29 13:31:08

+0

哎呀,是的,這就是我的意思哈哈。它不會讓我現在編輯我的評論出於某種原因... – gwin003 2013-04-29 13:33:59

17

我的問題是我期望某些操作會導致事件觸發。例如,將文件(單擊並拖動)從桌面移動到觀看位置不會引發事件,而是複製現有文件並粘貼其新副本(通過創建新文件到文件系統而不是簡單地移動現有的)導致Changed事件被提出。

我的解決方案是將每個NotifyFilter添加到我的FileSystemWatcher。通過這種方式,我會在FileSystemWatcher能夠通知我的所有情況下收到通知。

注意它不是完全直觀/明顯的哪些過濾器會通知您的具體情況。例如,我預計如果我包含FileName,那麼我將被告知對現有文件名稱所做的任何更改......而不是Attributes似乎可以處理這種情況。

watcher.NotifyFilter = NotifyFilters.Attributes | 
    NotifyFilters.CreationTime | 
    NotifyFilters.FileName | 
    NotifyFilters.LastAccess | 
    NotifyFilters.LastWrite | 
    NotifyFilters.Size | 
    NotifyFilters.Security; 
-1

我們剛剛有一個非常類似的問題,移動一個文件夾沒有觸發預期的事件。解決方案是複製整個文件夾,而不是僅僅移動它。

DirectoryCopy(".", ".\\temp", True) 

Private Shared Sub DirectoryCopy(_ 
     ByVal sourceDirName As String, _ 
     ByVal destDirName As String, _ 
     ByVal copySubDirs As Boolean) 

     ' Get the subdirectories for the specified directory. 
     Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName) 

     If Not dir.Exists Then 
      Throw New DirectoryNotFoundException(_ 
       "Source directory does not exist or could not be found: " _ 
       + sourceDirName) 
     End If 

     Dim dirs As DirectoryInfo() = dir.GetDirectories() 
     ' If the destination directory doesn't exist, create it. 
     If Not Directory.Exists(destDirName) Then 
      Directory.CreateDirectory(destDirName) 
     End If 
     ' Get the files in the directory and copy them to the new location. 
     Dim files As FileInfo() = dir.GetFiles() 
     For Each file In files 
      Dim temppath As String = Path.Combine(destDirName, file.Name) 
      file.CopyTo(temppath, False) 
     Next file 

     ' If copying subdirectories, copy them and their contents to new location. 
     If copySubDirs Then 
      For Each subdir In dirs 
       Dim temppath As String = Path.Combine(destDirName, subdir.Name) 
       DirectoryCopy(subdir.FullName, temppath, true) 
      Next subdir 
     End If 
    End Sub 
+0

的問題是,在C#中沒有vb的 – 2017-03-01 10:01:07

3

使用此setter啓用觸發器:

watcher.EnableRaisingEvents = true; 
+1

哦,我,非常感謝爲。令我驚訝的是,事件需要標誌。 – David 2017-04-26 16:47:44