homedaq
asked on
FileSystemWatcher - Detect Open Files
Hi All
I have used FileSystemWatcher to detect changes in a folder set and it's great but I'm presently expanding my application and I need to detect when a user opens a file. The FileSystemWatcher seems to imply that with the NotifyFilter you can specify to fire the Changed event on a LastAccess change. Should this not cause the event to fire on a file open. This is not happening for me does anyone know what I'm doing wrong. Here is a code snippet to show how I'm doing it.
watcher1.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryNam e | NotifyFilters.Attributes | NotifyFilters.LastAccess;
watcher1.Filter = "*.*";
watcher1.IncludeSubdirecto ries = true;
// Add event handlers.
watcher1.Changed += new FileSystemEventHandler(OnC hanged);
Is this possible ? If not does anyone know of a way to do this effectively. My problem is that the watcher could be looking at 100's or 1000's of files in nested folder so I don't want it to have to poll them all to see are they open as this would gobble up resources on the machine. My application is a background application.
Thanks in Advance
Regards,
I have used FileSystemWatcher to detect changes in a folder set and it's great but I'm presently expanding my application and I need to detect when a user opens a file. The FileSystemWatcher seems to imply that with the NotifyFilter you can specify to fire the Changed event on a LastAccess change. Should this not cause the event to fire on a file open. This is not happening for me does anyone know what I'm doing wrong. Here is a code snippet to show how I'm doing it.
watcher1.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryNam
watcher1.Filter = "*.*";
watcher1.IncludeSubdirecto
// Add event handlers.
watcher1.Changed += new FileSystemEventHandler(OnC
Is this possible ? If not does anyone know of a way to do this effectively. My problem is that the watcher could be looking at 100's or 1000's of files in nested folder so I don't want it to have to poll them all to see are they open as this would gobble up resources on the machine. My application is a background application.
Thanks in Advance
Regards,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This wouldn't be easy--you would have to do something like file-level auditing. Could I ask why you would need to know file access?
Bob
Bob
ASKER
I don't think I'm going to get an easy answer. I guess it roll up the sleeves time and forget about getting the lazy result.
I need to highlight to other connected users when a shared file is being accessed.
Regards,
I need to highlight to other connected users when a shared file is being accessed.
Regards,
Here is what I do, it seems to work well:
When a file changes, I start a timer for that file. I keep track of the file and the timer in a hash table.
When the timer expires, I lock the hash table and find the timer. Then, I try to open the file. If I get it, I assume that the changes to the file are done and I process the file. Otherwise I reset the timer for another time period (I use 5 seconds - way more than I probably need to.)
This seems to reliably capture files that are dropped in, and files that are created and expanded within my watched directory.
David
When a file changes, I start a timer for that file. I keep track of the file and the timer in a hash table.
When the timer expires, I lock the hash table and find the timer. Then, I try to open the file. If I get it, I assume that the changes to the file are done and I process the file. Otherwise I reset the timer for another time period (I use 5 seconds - way more than I probably need to.)
This seems to reliably capture files that are dropped in, and files that are created and expanded within my watched directory.
David
ASKER
Sounds like a plan. But I don't think it will help in my situation as I still need to detect when a file is opened. Not nessessarily changed. I can catch changes and new files and deletes easy with the file watcher but I was looking for file open which actually changes the files last accessed date but this doesn't trigger the NotifyFilters.LastAccess event.
I'm in the process of a rethink of the functionality required and hoping to drop this requirement.
Regards
I'm in the process of a rethink of the functionality required and hoping to drop this requirement.
Regards
I think Dan7el answered the original question and should get the credit with: "I've tested. It doesn't seem to fire like you want.'
David
David
ASKER
I know I have the fileInfo solution but I was hoping for a more subtle approach that would need a lot less processing. The problem is I would need to loop through these files repeatedly every few seconds and there could be 1000's like I said.
Regards,