Click here to Skip to main content
15,886,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have a windows application which is using a filesystemwatcher which listens to the folder and when ever the xml file created, it will parse the file and inserts to the database.

The problem is it works fine when the first file is created, and when the second file created, it shows error, "The process cannot access the file because it is being used by another process".

I cannot dispose the filesystemwatcher object because it should listen to the folder always.

I have posted the code below. Kindly help me to resolve the issue.

C#
private void button1_Click(object sender, EventArgs e)
        {
            ListenToFolder(@"F:\EscherData\");
        }

        private void ListenToFolder(string path)
        {
            try
            {

                    FileSystemWatcher fs = new FileSystemWatcher(path);

                    fs.IncludeSubdirectories = false;


                    fs.Created += new FileSystemEventHandler(OnFileCreate);


                    fs.EnableRaisingEvents = true;





            }
            catch (Exception e)
            {

                //MessageBox.Show(e.ToString());
            }
        }

        private void OnFileCreate(object e, FileSystemEventArgs ev)
        {

            try
            {

                ReadXmlData(ev.FullPath);


            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.ToString());
            }






        }
Posted
Comments
[no name] 18-Sep-12 14:45pm    
Created does not mean that the file has been completly written and is ready for something else to access it.
Ahamed Azeem 18-Sep-12 14:52pm    
oh okay, so which event should i use instead

1 solution

The problem is that the FileSystemWatcher tells you immediately when the file was created. It doesn't wait for the file to be released. The system is still creating the file when you already have the message, which is why you are getting that error message. Basically, you need to wait your turn. Take the event and then have a utility watch that file until it is free for reading. Here is a StackOverflow question that has a few different ways to do this:

http://stackoverflow.com/questions/50744/wait-until-file-is-unlocked-in-net[^]
 
Share this answer
 
Comments
Ahamed Azeem 18-Sep-12 15:06pm    
Thanks Tim.

Also when i debug the code using break point, it works, thats why I gone mad. I will try your solution.
Tim Corey 18-Sep-12 15:08pm    
That's logical. The file isn't locked for very long. To the computer, it is a long time but when you step through the code, the system has enough time to finish creating the file between the time the event fires and when you are able to step to the place where the file is read.
Ahamed Azeem 18-Sep-12 15:19pm    
Thanks Tim. It worked. You saved my hours
Sergey Alexandrovich Kryukov 18-Sep-12 16:38pm    
My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900