FileSystemWatcher没有触发事件

时间:2013-04-29 12:35:44

标签: c# filesystemwatcher

出于某种原因,我的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));
    }
}

5 个答案:

答案 0 :(得分:22)

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

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

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

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

答案 1 :(得分:21)

您似乎在安装方法中将FileSystemWatcher创建为本地变量。这当然会超出方法的最后范围,并且可能会在那时整理,从而移除手表。

尝试在持久化的位置创建FSW(例如程序级变量)并查看是否将其排除在外。

答案 2 :(得分:7)

使用此setter启用触发器:

watcher.EnableRaisingEvents = true;

答案 3 :(得分:0)

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

fileSystemWatcher.IncludeSubdirectories = true;

答案 4 :(得分:-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