繁体   English   中英

FileSystemWatcher 不触发事件

[英]FileSystemWatcher not firing events

出于某种原因,我的FileSystemWatcher没有触发任何事件。 我想知道何时在我的目录中创建、删除或重命名新文件。 _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));
    }
}

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

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

我的问题是我希望某些操作会导致FileSystemWatcher Changed事件触发。 例如,将文件(单击并拖动)从桌面移动到观看位置不会引发事件,而是复制现有文件并粘贴它的新副本(通过在文件系统中创建新文件而不是简单地移动一个现有的)导致Changed事件被引发。

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

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

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

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

watcher.EnableRaisingEvents = true;

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

fileSystemWatcher.IncludeSubdirectories = true;

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM