2

I'm using the below code to listen for change events of a file i download off a server and open. however the change event only gets fired the first time the file is saved, then on subsequent saves the file watcher does not fire the change events?

Can anyone see whats going on?

private FileSystemWatcher StartWatchingFile()
{
    fw = new FileSystemWatcher();
    fw.Path = this.directoryLocation;
    fw.Filter = this.Filename;

    fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite;

    // Add event handler
    fw.Changed += new FileSystemEventHandler(fw_Changed);

    // Open file        
System.Diagnostics.Process.Start(this.CreateAbsoluteFilePath(this.Filename));

    // Begin watching.
    fw.EnableRaisingEvents = true;

    return fw;
}

//************************  

    void fw_Changed(object sender, FileSystemEventArgs e)
    {
        MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
    }

EDIT: The StartWatchingFile() now returns the filewatcher which is kept in a class that will not be garbage collected, just to make sure i'm holding the whole class as thought the fw_changed() function might not be able to be called. So the whole class is now not getting garbage collectioned. The class is held in a ArrayList which is a public member of a class

Regards,

Jon

Jon
  • 4,295
  • 6
  • 47
  • 56
  • The code you posted is not catching the Renamed or Deleted events. I take it the file is not getting renamed or deleted? – Paul Williams Aug 21 '09 at 20:04

2 Answers2

1

Is it reproduceable that it always works for the first time?

If not, the FileSystemWatcher might have been collected by the garbage collector in the meantime after StartWatchingFile has finished because it's declared locally.

If so, does the process you're starting probably lock the file so the file actually isn't modified?

dtb
  • 213,145
  • 36
  • 401
  • 431
  • I'm now holding the whole class in a varible of another class so it won't get garbage collected but it still only fires once. I think your right about the file being locked by the application that opens it, but why does the file watcher fire on the first save only? – Jon Aug 21 '09 at 20:22
  • I don't think that it would be collected as the event handler is a reference to the object. – Ed S. Aug 21 '09 at 20:43
  • ...Though it should be a class level variable anyhow. – Ed S. Aug 21 '09 at 20:44
  • What should be class level variable the event handler? how would i do that? – Jon Aug 21 '09 at 20:46
  • No, the FileSystemWatcher. If it is being used/referenced outside of a method you should probably hoist it up into the class definition. You already did this I see. – Ed S. Aug 21 '09 at 20:55
1

I'm sorry I can't answer your question specifically. In general, I'll contribute that, if you use it enough, you'll learn that FileSystemWatcher is not reliable. Microsoft Connect shows multiple problems with it. I agree with Jason Jackson's take on FileSystemWatcher.

Community
  • 1
  • 1
lance
  • 16,092
  • 19
  • 77
  • 136
  • what root would you suggest? The scenario is I'm download a file off a server, which the user is allowed to edit. Once the users has finished editting the modified file has to be uploaded to the server again without any input from the user. The caught is the user may close the file but not the program it is editted in? Thanks for your input. – Jon Aug 21 '09 at 20:44
  • I can't speak knowledgeably to the concern about the other editing software having a lock (or whatever) on the file you need to upload. But, regarding "watching" the file for changes, I'd poll the filesystem and compare what you saw before (last modified timestamp, filesize, whatever) to what you see now, and "upload to the server" when you see the change you were looking for. It's ugly, but it's reliable. – lance Aug 21 '09 at 21:25