0

I am using File Watcher (.NET) and always getting an below error- file is being used by another process.

The above error only comes in Network sharing, i.e. Watch Directory is shared and if someone puts the file into that directory then only this error occurs. It works fine when I put the file from my m/c to watch directory(locally).

This is how my below code looks, the error comes in first line itself. I have tried all the solutions given on net but nothing seems to working. The only solution worked when I put Thread.Sleep(500), but after that it is unable to pick the next file. Any suggestion would be a great help.

      try
        {

         using (Stream stream = File.Open(xmlPath, FileMode.Open, FileAccess.Read,             FileShare.Read))
          {
                    XmlTextReader xmlTextReader = new XmlTextReader(stream);
                    document = XDocument.Load(xmlTextReader);
                    ........ // my code
                    xmlTextReader.Close();
                    stream.Close();

           }
        }
       catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.ToString(), EventLogEntryType.Error);

            }
Punit
  • 1,347
  • 3
  • 20
  • 39
  • 1
    stream.close() is redundant in your code.using will do that for you – Prabhu Murthy Oct 04 '12 at 09:58
  • 1
    @CodeIgnoto is correct, also `XmlTextReader` implements `IDisposable` so you can wrap that up in a `using` as well. – James Oct 04 '12 at 10:00
  • @CodeIgnoto - yeah i know i have removed that code. – Punit Oct 04 '12 at 10:00
  • yeah but even if i remove those line the error still comes. – Punit Oct 04 '12 at 10:00
  • @Punit that advice isn't about answering your question it's more about general housekeeping. To be honest, the answer is in your exception "*file is being used by another process*" - either someone still has the file open or an application does. – James Oct 04 '12 at 10:03
  • You should open the file on a different thread where the sleep doesnt do any harm. When a new file appears on the Network share you cannot deterministically tell when the Operation that created the file is finished and you can Access it. – Daniel Schlößer Oct 04 '12 at 10:03

1 Answers1

1

I think you will find that the the file is not acessable hence why the wait works..

Take a look at this code and see if this helps...

// Consider having a List<String> named _changedFiles

private void OnChanged(object source, FileSystemEventArgs e)
{
    lock (_changedFiles)
    {
        if (_changedFiles.Contains(e.FullPath))
        {
            return;
        }
    }

    // do your stuff

    System.Timers.Timer timer = new Timer(1000) { AutoReset = false };
    timer.Elapsed += (timerElapsedSender, timerElapsedArgs) =>
    {
        lock (_changedFiles)
        {
            _changedFiles.Remove(e.FullPath);
        }
    };
   timer.Start();
}

from: FileSystemWatcher Changed event is raised twice

Community
  • 1
  • 1
  • @ Mathew- Hope this will work for OnCreate as well, Where are you adding the file name to the list, and also I am setting the EnableRaisingEvents to true, is it ok? – Punit Oct 04 '12 at 10:19
  • Hi, yes it should be set to true. Take a look at a microsoft example which is quite clear actually. http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.enableraisingevents.aspx It should work for the oncreate or any event really. Basically you just need to delay the file access until you can get a lock on the file that you need to open. This is the root cause of the problem I believe. Anyway I hope this helps to clarify the problem and give you a solution? Regards Matthew Zielonka –  Oct 04 '12 at 10:35
  • Great, pleased I could help :) –  Oct 04 '12 at 18:51