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.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,
         
homedaqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
homedaqAuthor Commented:
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
Bob LearnedCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

homedaqAuthor Commented:
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
anyoneisSoftware DeveloperCommented:
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
homedaqAuthor Commented:
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
anyoneisSoftware DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.