Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I detect when a directory or file changes without constant scanning

Other than reading all the files and comparing them with a previous snapshot, is there a way to detect when a directory changes in C# with Windows? I don't mind PInvoke if that's what it takes.

EDIT The FileSystemWatcher class is great, but one gotcha is that you must kick off any work in the callback notification to a background task/thread to avoid blocking which will cause dropped events.

like image 801
jjxtra Avatar asked Jul 09 '10 18:07

jjxtra


People also ask

How do I monitor changes to a folder?

To monitor changes to a folder, you need to open the Event Viewer. In Windows search box, type “Event Viewer” and open the tool from the result. Go to “Windows Logs” and then “Security”. This will open a list of the recent activities on the middle panel.

How do I monitor the file and folder changes in Windows?

Login to ADAudit Plus → Go to File Audit tab → Under File Audit Reports → navigate to All File/Folder Changes report. Select the time period for which you want to track the changes made and the domain that the file server belongs to. The details you will find in this report are: Name of the file/folder that was changed.

What is a file watcher?

File Watcher is an IntelliJ IDEA tool that allows you to automatically run a command-line tool like compilers, formatters, or linters when you change or save a file in the IDE.

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher lets you detect several types of changes in a directory or file, like the 'LastWrite' date and time, changes in the size of files or directories etc. It also helps you detect if a file or directory is deleted, renamed or created.


1 Answers

Use the FileSystemWatcher class - it does what you want. It won't tell you which bytes in the file changed, but it will tell you which files have changes.

From the doc:

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

To watch for changes in all files, set the Filter property to an empty string ("") or use wildcards ("."). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the Filter property to "*.txt".

There are several types of changes you can watch for in a directory or file. For example, you can watch for changes in Attributes, the LastWrite date and time, or the Size of files or directories. This is done by setting the NotifyFilter property to one of the NotifyFilters values. For more information on the type of changes you can watch, see NotifyFilters.

You can watch for renaming, deletion, or creation of files or directories. For example, to watch for renaming of text files, set the Filter property to "*.txt" and call the WaitForChanged method with a Renamed specified for its parameter.

like image 167
LBushkin Avatar answered Oct 21 '22 03:10

LBushkin