Member Avatar for kobalt

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!

Recommended Answers

All 2 Replies

Member Avatar for kobalt

Ok, I have a workaround that seems effective enough:

private void fileMAwatch_Changed(object sender, FileSystemEventArgs e)
        {
                bool fileInUse = true;
                while (fileInUse == true)
                {
                    try
                    {
                        FileStream fs;
                        fs = File.OpenRead(_sPath);
                        fileInUse = false;
                        fs.Close();
                    }
                    catch (IOException)
                    {
                        fileInUse = true;
                    }
                }
// follow on with the rest of the code....

Can anyone see any problems with this? It's a little cumbersome, however, it hasnt crashed yet for what I need it to do....

There is nothing wrong with the approach you have used. In fact, Changed event is fired when a file has been modified in the directory that is being monitored. This event may be fired multiple times, even when only one change to the content of the file has occurred.

Text from MSDN page :
The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.

Have a look at article - http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.