tags:

views:

483

answers:

2

Hi,

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

+1  A: 

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
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
Please update your question to reflect your code changes.
dtb
I've just updated it
Jon
I don't think that it would be collected as the event handler is a reference to the object.
Ed Swangren
...Though it should be a class level variable anyhow.
Ed Swangren
What should be class level variable the event handler? how would i do that?
Jon
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 Swangren
A: 

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.

lance
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
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