Search code examples
c#windowsfilesystemwatcher

How to get more information about FileSystemWatcher events?


The code from https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-7.0 reports about ten events per new file, something like "Created, Deleted, Created, Changed, Changed", together with the path, but this is not very useful. The "ChangeType" property gives the same basic information.

How to get more information? Something that specified the type of change would be good, especially something that would say Windows finished the standard procedure of downloading a file / putting a file in a folder.


Solution

  • There isn't 'more information' of the kind you want, because FileSystemWatcher works at the rather low level of file system operations (duh) and has no notion of what the user or user-controlled applications are doing: FileSystemWatcher basically observes OS API calls. What's more, nothing forces different browsers or applications, using different standard libraries and/or runtimes such as .NET or Python, on top of OS API to employ the same sequence of file system operations to download a file or put a file in a folder. To give a simple example, some downloaders download directly to the destination file, but many downloaders download to a temporary file and rename it to the destination file after the download is over, and some create a zero-length destination file while the download to the temporary file is ongoing and others don't. You will have to make an educated guess, based on your use case(s), to interpret sequences of events relating to the same file as generated by a high-level operation.