I'm using C# for a mini project of mine, I am trying to monitor files that are changed, Deleted, And/or created. And export that to a file. But I am not quite sure how to monitor files. Any ideas?
Asked
Active
Viewed 1.1k times
3 Answers
14
You can use the FileSystemWatcher
class to monitor file creation, modifications, deletions and renames.
You have the following events available:

Christian C. Salvadó
- 807,428
- 183
- 922
- 838
3
you can only monitor one Directory with FSW, if you want more you may have to make multiple instances of FSW. It monitors the directory not the file; if the file is moved (changed
+created
; changed
+renamed
) then you need another FSW for that destination folder.

Paradigm
- 193
- 1
- 4
- 11
-
1According to the following article, you can tell the FileSystemWatcher to monitor subdirectories by setting the FileSystemWatcher.IncludeSubdirectories property to true. https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.includesubdirectories(v=vs.110).aspx – AdamantineWolverine Jul 11 '16 at 04:23
2
Yep, FileSystemWatcher
will do this. Be careful what you wish for, because this thing watches everything!
Have a look here, it explains how you it can fire multiple events (eg when a file is moved, firing an OnChange and OnCreate

Christian Payne
- 7,081
- 5
- 38
- 59