FileSystemWatcher not firing events

后端 未结 5 1547
感动是毒
感动是毒 2020-12-20 11:12

For some reason, my FileSystemWatcher is not firing any events whatsoever. I want to know any time a new file is created, deleted or renamed in my directory.

相关标签:
5条回答
  • 2020-12-20 11:23

    My problem was that I expected certain actions to cause the FileSystemWatcher Changed event to fire. For example, moving a file (clicking and dragging) from the desktop to the watched location did not raise an event but copying an existing file and pasting a new copy of it (there by creating a new file to the file system and not simply moving an existing one) caused the Changed event to be raised.

    My solution was to add every NotifyFilter to my FileSystemWatcher. This way I am notified in all cases where the FileSystemWatcher is able to notify me.

    NOTE that it isn't entirely intuitive/obvious as to which filters will notify you for specific cases. For example, I expected that if I included FileName that I would be notified of any changes to an existing file's name...instead Attributes seem to handle that case.

    watcher.NotifyFilter = NotifyFilters.Attributes |
        NotifyFilters.CreationTime |
        NotifyFilters.FileName |
        NotifyFilters.LastAccess |
        NotifyFilters.LastWrite |
        NotifyFilters.Size |
        NotifyFilters.Security;
    
    0 讨论(0)
  • 2020-12-20 11:24

    We just had a very similar problem, where moving a folder did not trigger the expected events. The solution was to hard copy the entire folder, rather than just moving it.

    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 讨论(0)
  • 2020-12-20 11:26

    My issue was that I expected it watches subdirectories also but it doesn't do it by default. If you would like to monitor also subdirectories then set IncludeSubdirectories property to true (it's false by default):

    fileSystemWatcher.IncludeSubdirectories = true;
    
    0 讨论(0)
  • 2020-12-20 11:28

    You seem to be creating the FileSystemWatcher as a local variable in the setup method. This will of course go out of scope at the end of the method and may well be getting tidied up at that point, thus removing the watches.

    Try creating the FSW at a point where it will be persisted (eg a program level variable) and see if that sorts you out.

    0 讨论(0)
  • 2020-12-20 11:39

    Use this setter to enable the trigger:

    watcher.EnableRaisingEvents = true;
    
    0 讨论(0)
提交回复
热议问题