Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# file/folder monitor

I already managed to see file and folder changes with the FileSystemWatcher.

My problem is that I can't make a difference between files and folder. It's possible that a file and a folder have the same path names.

For the delete event I even can't use a dirty workarround with testing File.Exists(path) or Directory.Exists(path) because the file/folder is already deleted when the method is called.

Maybe this object has the info I need but I didn't found it:

FileSystemEventArgs e

I only want to know if the changed item was a file or a folder.

like image 361
fpdragon Avatar asked Oct 27 '25 02:10

fpdragon


1 Answers

Assuming it's on a NTFS volume I think you could do what you need by looking at the Change Journals. Specifically the FSCTL_READ_USN_JOURNAL control code and looking at the FileAttributes of the USN_RECORD to see if it's a FILE_ATTRIBUTE_DIRECTORY.

You can find a sample here (in C++, but could possibly translate to C# or otherwise maybe just write a small C++ dll to call from your app): Walking a Buffer of Change Journal Records

like image 131
Hans Olsson Avatar answered Oct 28 '25 14:10

Hans Olsson