Hi,
I'm having trouble implementing a FileSystemWatcher.Changed event. I have been searching for solutions over the past week and am of the opinion (correct me if I'm wrong!) that what I want to do may not be entirely possible.
I have setup a FileSystemWatcher to monitor 1 file. This file is written to by an external application intermittently (by the user). When the file is saved, my application will read some of the information within it and then display in a datagrid.
The problem is that the filesize may vary, which causes the savetime to vary and my application then crashes due to the file still being open or in-use when the ap attempts to access it. Somehow I need to delay the time from the event Changed is triggered or for the event to flag when the file isnt in use.
Currently, the only code I have is an attempt to open the file with a catch,
private void fileMAwatch_Changed(object sender, FileSystemEventArgs e)
{
try
{
File.Open(txtMAworkbook.Text, FileMode.Open);
}
catch (Exception ex)
{
throw ex;
}
}
however, this 'catch' doesnt... and I get the following error
"TargetInvocationException was unhandled", delving deeper Inner Exception: Access the the path C"\blah blah blah is denied" or "The process cannot access the file because it is being used by another process"
Is there any way to check that the file is NOT still open or being accessed by anything in order that my application can read its contents?
Thanks for your help!