2013-04-29 78 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