C# FileSystemWatcher未触发事件

C# FileSystemWatcher未触发事件,c#,filesystemwatcher,C#,Filesystemwatcher,出于某种原因,我的FileSystemWatcher没有触发任何事件。我想知道在我的目录中创建、删除或重命名新文件的任何时间\u myFolderPath设置正确,我已检查 这是我目前的代码: public void Setup() { var fileSystemWatcher = new FileSystemWatcher(_myFolderPath); fileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess |

出于某种原因,我的
FileSystemWatcher
没有触发任何事件。我想知道在我的目录中创建、删除或重命名新文件的任何时间<代码>\u myFolderPath设置正确,我已检查

这是我目前的代码:

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));
    }
}

您似乎正在将FileSystemWatcher创建为setup方法中的局部变量。当然,在方法结束时,这将超出范围,并可能在那时得到整理,从而移除手表


尝试在FSW将被持久化的点(例如程序级变量)创建FSW,看看这是否能解决问题。

我的问题是,我预期某些操作会导致触发
FileSystemWatcher
事件。例如,将文件从桌面移动到关注的位置(单击并拖动)不会引发事件,但复制现有文件并粘贴其新副本(通过在文件系统中创建新文件而不是简单地移动现有文件)会引发
更改的
事件

我的解决方案是将每个
NotifyFilter
添加到我的
FileSystemWatcher
。这样,在
FileSystemWatcher
能够通知我的所有情况下,我都会收到通知

注意对于特定情况下哪些过滤器会通知您,这并不完全直观/明显。例如,我希望如果我包括
文件名
,我会收到现有文件名的任何更改的通知……而
属性
似乎可以处理这种情况

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

我们刚刚遇到了一个非常类似的问题,移动文件夹不会触发预期的事件。解决方案是硬拷贝整个文件夹,而不仅仅是移动它

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

使用此设置器启用触发器:

watcher.EnableRaisingEvents = true;

我的问题是,我希望它也监视子目录,但默认情况下它不会这样做。如果要同时监视子目录,请将IncludeSubdirectories属性设置为true(默认为false):


您做了什么来验证这些事件是否会触发?我已经在我的目录中手动创建、重命名和删除了文件。无任何触发。本地驱动器,更具体地说是AppData/Roaming文件夹您确定
\u myFolderPath
是正确的路径吗?因为您的代码看起来很好,与签出时的示例非常相似,所以请尝试将
FileSystemWatcher
移动到一个私有局部变量,现在它的工作似乎更加一致了。@gwin003它是一个局部变量。我猜你是说你把它搬到了私人领域?哦,是的,这就是我的意思哈哈。由于某些原因,它现在不允许我编辑我的评论…你只能在之后的有限时间内(我想是5分钟)编辑评论。很高兴这有帮助。FSW需要注意的另一点是,它们不能在网络驱动器上触发事件。从上面的评论来看,这与您现在的情况无关,但如果您愿意,这是一个大难题。:)@adospace:问题出在c#而不是vbOh天哪,非常感谢。让我惊讶的是,活动需要一面旗帜。哦,哇。。。这花了我一个小时的时间:奇怪的选择,默认设置为
false
。这些事件难道不是你首先想使用
FileSystemWatcher
的主要原因吗?@theberserker,我认为默认为false是最明智的选择,因为你可能想在给定时间设置watcher,但只能在完成其他安装工作后才启用它。
fileSystemWatcher.IncludeSubdirectories = true;