?
Solved

FileSystemWatcher - Detect Open Files

Posted on 2006-04-26
10
Medium Priority
?
894 Views
Last Modified: 2008-01-09
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.DirectoryName | NotifyFilters.Attributes | NotifyFilters.LastAccess;
 
watcher1.Filter = "*.*";
watcher1.IncludeSubdirectories = true;

// Add event handlers.
watcher1.Changed += new FileSystemEventHandler(OnChanged);

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,
         
0
Comment
Question by:homedaq
7 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 total points
ID: 16549773
I've tested.  It doesn't seem to fire like you want.

I don't know of any other way then creating a FileInfo object and using the static Directory class to open a directory, get all the files, and check for the FileInfo object's LastAccess:

            string myPath = @"C:\My Documents\";
            string[] files = Directory.GetFiles(myPath);
            while (doIt)
            {
                foreach (string filename in files)
                {
                    myFile = new FileInfo(filename);
                    myFile.Refresh();
                    if (myFile.LastAccessTime.Ticks > (DateTime.Now.Ticks - 10))
                    {
                        MessageBox.Show(myFile.Name + " was opened.");
                    }
                }
            }
But the loop pretty much takes over the whole process.  You might want to sleep in between and perhaps do some threading.
0
 

Author Comment

by:homedaq
ID: 16550992
Thanks for the response.
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,
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16552833
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:homedaq
ID: 16552892
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,
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16675428
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
0
 

Author Comment

by:homedaq
ID: 16678981
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
0
 
LVL 11

Expert Comment

by:anyoneis
ID: 16938235
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
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question