38

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

5 回答 5

36

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

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

于 2013-04-29T12:51:28.007 回答
30

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

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

请注意,哪些过滤器会在特定情况下通知您并不完全直观/明显。例如,我希望如果我包括在内FileName,我会被通知现有文件名的任何更改......而不是Attributes似乎处理这种情况。

watcher.NotifyFilter = NotifyFilters.Attributes |
    NotifyFilters.CreationTime |
    NotifyFilters.FileName |
    NotifyFilters.LastAccess |
    NotifyFilters.LastWrite |
    NotifyFilters.Size |
    NotifyFilters.Security;
于 2014-03-12T20:55:46.163 回答
21

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

watcher.EnableRaisingEvents = true;
于 2017-03-01T10:14:06.137 回答
0

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

fileSystemWatcher.IncludeSubdirectories = true;
于 2020-08-23T19:50:26.230 回答
-2

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

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
于 2016-08-23T08:57:28.147 回答