Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

FileSystemWatcher for opening files?

Back to Forum: Tech Off
  • User profile image
    TimP

    Is there any way to be notified when files are opened by a user? Something like the FileSystemWatcher class, except with an "Opened" event. I just need the file name of the file that's opened.

    For example, User double clicks on "myfile.txt" on the Desktop and Notepad is launched. I need some way to be notified that C:\Documents and Settings\User\Desktop\myfile.txt has been opened.

    Thanks!

  • User profile image
    TommyCarlier

    You can set the NotifyFilter-property to LastAccess, and catch the Changed-event. Like this:

    FileSystemWatcher lWatcher = new FileSystemWatcher(@"C:\Documents and Settings\User\Desktop", "myfile.txt");
    lWatcher.NotifyFilter = NotifyFilters.LastAccess;
    lWatcher.Changed += new FileSystemEventHandler(HandlerWatcherChanged);

    void HandlerWatcherChanged(object sender, FileSystemEventArgs e)
    {
       // file has been accessed
    }

  • User profile image
    Minh

    Just a note. FileSystemWatchers don't work on Windows 9x.

  • User profile image
    JohnAskew

    TommyCarlier wrote:
    You can set the NotifyFilter-property to LastAccess, and catch the Changed-event. Like this:
    FileSystemWatcher lWatcher = new FileSystemWatcher(@"C:\Documents and Settings\User\Desktop", "myfile.txt");
    lWatcher.NotifyFilter = NotifyFilters.LastAccess;
    lWatcher.Changed += new FileSystemEventHandler(HandlerWatcherChanged);

    void HandlerWatcherChanged(object sender, FileSystemEventArgs e)
    {
       // file has been accessed
    }



    Groovy. I was wondering about that ability but hadn't tried to find it.

    Tommy is my code example hero.

  • User profile image
    Yggdrasil

    Minh wrote:
    Just a note. FileSystemWatchers don't work on Windows 9x.


    Just another note. FileSystemWatchers don't work too well anywhere else as well.

    Be careful if you're monitoring lots of changes at once. I had lots of problems with multiple events popping for single operations, events coming up in the wrong order and so forth. Shouldn't be a problem if you have a second or two between events.

  • User profile image
    Rossj

    Yggdrasil wrote:
    
    Be careful if you're monitoring lots of changes at once. I had lots of problems with multiple events popping for single operations, events coming up in the wrong order and so forth. Shouldn't be a problem if you have a second or two between events.


    I've seen this too, primarily with file creation events - typically I have had to keep track of the recent events and ignore dupes - also trying to get an exclusive lock on the file to ensure that the file copy (or write) has actually finished because the creation event is raised before the file is completely there.

  • User profile image
    TommyCarlier

    Yeah, the FileSystemWatcher has quite a few quirks. At work, I've created a wrapper class around it to more reliably detect created files, and make sure they are no longer locked when you try to read the files.

  • User profile image
    CRG1970

    TommyCarlier said:
    Yeah, the FileSystemWatcher has quite a few quirks. At work, I've created a wrapper class around it to more reliably detect created files, and make sure they are no longer locked when you try to read the files.

    Tommy, high.

    This appears to be a very common problem.  I can see you answered this a while back, but was wondering if you could share with us more info on how you created that wrapper.  I think many of us will find it very, very useful.

  • User profile image
    TommyCarlier

    CRG1970 said:
    TommyCarlier said:
    *snip*

    Tommy, high.

    This appears to be a very common problem.  I can see you answered this a while back, but was wondering if you could share with us more info on how you created that wrapper.  I think many of us will find it very, very useful.

    I put the wrapper on my blog a while ago: http://tommycarlier.blogspot.com/2007/01/filecreationwatcher.html

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.