Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one :-D

As you know we can use FileSystemWatcher class to monitor events of a directory or a file. The events are only Create, Delete, Change, Rename, Error, Dispose where as I want to monitor all events. For example I want to get the path of directory which is opened or when a file is clicked. Something like filesystem driver but in C#.

So what do you suggest me?


What you can do easily is monitoring mouse and keyboard activity:
Processing Global Mouse and Keyboard Hooks in C#[^]

But what you are asking is not possible unless you hook into the process you are watching (dangerous and not to be recommanded).
Share this answer
Sergey Alexandrovich Kryukov 28-Feb-11 11:19am    
I agree, my 5.
Sergey Alexandrovich Kryukov 28-Feb-11 11:25am    
There is more to it, will you see my answer?
I agree with Olivier. You need to use combination of FileSystemWatcher and the global hook. You will receive events from hook and file system without any synchronization, so it will be very hard to figure out what click caused what file system event. Also, timing of the file system notification is an issue.

Please see these articles: FileSystemWatcher - Pure Chaos (Part 1 of 2)[^] and FileSystemWatcher - Pure Chaos (Part 2 of 2)[^].

If you explain ultimate goals of your design, chances are, you could get better advice. You approach looks strange. Why do you want all this activity?

Share this answer
Olivier Levrey 28-Feb-11 12:32pm    
Right. My 5. Even though I don't really understand whar OP wants, he's probably choosing the wrong way.
Sergey Alexandrovich Kryukov 28-Feb-11 12:56pm    
Thank you, Olivier.
Possibly, we do not know exactly.
Coder-123456 2-Mar-11 3:34am    
You are right it's better to explain more. I want to write an anti-virus in C#, before it I wrote a filesystem driver with WDK and used it in VC but with WDK I didn't know how to work graphically(forms, ...) it was hard for me so I decided to choose C#. Now I want do the same thing witch I could do with a filesystem driver. Is it possible to write that driver in C# again?
Sergey Alexandrovich Kryukov 2-Mar-11 19:44pm    
Yes, after we finally get to the post-windows systems where device drivers are written on managed mode. Why not. C# is only available for .NET platform, so with Windows and WDK we don't have a chance. I' hoping to see the end in the tunnel. Now there is Microsoft Singularity, CosmOS, SharpOS moving in this direction. Right now, a driver's level in C# is not an option.

Still, in principle you can develop driver-level approach and develop API to it which you could use in .NET via P/Invoke (or C++/CLI).

The FileSystemWatcher doesn't track mere file opening. It just tracks physical changes to a file (create, change, delete, and that kind of thing). The only thing I can think of that will come close to doing what you want is to maintain a watch of open file handles in the system. You MIGHT be able to get that info using WMI, or some system debugging mechanism. Google is your friend.

EDIT ============

I found this googling for "c# file handle count wmi":

Listing Used Files[^]
Share this answer
Sergey Alexandrovich Kryukov 28-Feb-11 12:55pm    
Good point, my 5. I also referenced your articles on FileSystemWatcher in my answer as your information is good to know. (I did it because you did not post your Answer at that time yet.)
#realJSOP 28-Feb-11 13:04pm    
I didn't mention them myself because they don't really address what I think the guy wants to do (or at least not all of it).
Sergey Alexandrovich Kryukov 28-Feb-11 19:49pm    
Agree, but because the whole idea is questionable, as I say in my asnwer, you article can serve an extra warning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900