5

I'm getting an exception: The process cannot access the file.

Here's the code:

if (!Monitor.TryEnter(lockObject))
    return;
try
{
    watcher.EnableRaisingEvents = false;
    try
    {
        XmlDocument xdoc = new XmlDocument();
        xdoc.Load(FileName);
        xdoc = null;
    }
    catch (XmlException xe)
    {
        using (StreamWriter w = File.AppendText(FileName))
        {
            Console.WriteLine(xe);
            w.WriteLine("</test>");
            w.WriteLine("</testwrapper>");
        }
    }
    System.Threading.Thread.Sleep(2000);
    XPathDocument myXPathDoc = new XPathDocument(new StreamReader(FileName, System.Text.Encoding.GetEncoding("windows-1256")));
    XslCompiledTransform myXslTrans = new XslCompiledTransform();
    myXslTrans.Load("D:/GS/xsl/test.xsl");
    XmlTextWriter myWriter = new XmlTextWriter(destinationFile, null);
    myWriter.Formatting = Formatting.Indented;
    myWriter.Indentation = 4;
    myXslTrans.Transform(myXPathDoc, null, myWriter);
    myWriter.Close();
}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());
}
finally
{
    Monitor.Exit(lockObject);
    watcher.EnableRaisingEvents = true;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
}

The code was working perfectly before I added these lines. These are mainly for testing if the xml file is without the closing tags (which) I normally get then add the tags. After I added the following code it started giving me this exception.

try
{
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(FileName);
    xdoc = null;

}
catch (XmlException xe)
{
    using (StreamWriter w = File.AppendText(FileName))
    {
        Console.WriteLine(xe);
        w.WriteLine("</test>");
        w.WriteLine("</testwrapper>");
    }
}             

What could be wrong here ?

EDIT: Error I'm getting

The process failed: System.IO.IOException: The process cannot access the file 'z :\TF_B1BBA.xml' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea n useLongPath) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofO bjectToReturn) at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) at System.Threading.CompressedStack.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCl eanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.CompressedStack.Run(CompressedStack compressedStack, Cont extCallback callback, Object state) at System.Xml.XmlTextReaderImpl.OpenUrl() at System.Xml.XmlTextReaderImpl.Read() . at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean prese veWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.Load(String filename) at GSelInterface.Program.convert(Object source, FileSystemEventArgs f) in C:\ Documents and Settings\Administrator\Desktop\ConsoleApplication1\ConsoleApplicat ion1\Program.cs:line 178

6
  • is lockobject and or watcher objects working in some way on the same file? they can be the reason of the filelock Commented Apr 4, 2013 at 13:55
  • Yes, I have watcher object monitoring that folder. After it picks it up, i then turn it the watcher false, then pass it to verify the xml is perfect (as in the second code) and then apply the xml transformations. But I do put the watcher as false before I do all this, so there are slight to no chances of it interfering with file processing being done. Maybe I'm wrong. Help me understand. Commented Apr 4, 2013 at 14:04
  • I tried removing the lockobject for threads which I'm using here, but the error is still the same. Commented Apr 4, 2013 at 14:08
  • The simply code you added will work. I tried it as is, even if I don't think best practice to do somethinig inside a catch block, and I'm not receiving a file error. So it's something else locking the file for writing, maybe outside the snipped you post Commented Apr 4, 2013 at 14:18
  • Here's the complete code: pastebin.com/rFqZVB0H Commented Apr 4, 2013 at 14:38

5 Answers 5

4

In your try block you have opened the file. You need to close it.

XmlDocument xdoc = new XmlDocument();
xdoc.Load(FileName);

Follow this example.

http://msdn.microsoft.com/en-us/library/zcsyk915.aspx

Sign up to request clarification or add additional context in comments.

10 Comments

I have put to null, shouldn't that work. If not what is the best way of unloading it
You can call garbage collector to collect memory.
Yes, I removed xdoc=null. and put in GC.collect(). It's still the same error
Remove both lines, xdoc=cull and GC.collect(), and then try.
|
4

It may be because of the watcher (then FileShare.ReadWrite is the important part).

Try:

XmlDocument xdoc = new XmlDocument();
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
xdoc.Load(fs);

2 Comments

Sure, I can try this out, But I have solution on hand for the moment, please see my answer. Thank you buddy for supporting.
Whatever works for you :) It's just I had a similar problem once (FileSystemWatcher + opening a file in the monitored folder + random "The process cannot access the file" exception) ant it worked for me. I checked out your answer and I guess it can be also done that way.
0

You are trying to write on "Filename" file that is already open in the try block.

Edit 1:

It seems the lock is set by the process that is saving the file. When convert() is fired, filesystem has still not finished to save the file. It happens expecially if you have a big xml. If you add a sleep just before trying to write the file, exception is not raised.

This is a quick&dirty patch.

If xml files are saved with an high frequency, you need to add some kind of lock to the xml file changed.

Edit 2:

Try also to remove watcher's event before doing stuff, and add again after everything is done, so you prevent multiple events to be fired. Not so sure that EnableRaisingEvents = false will work in the right way. See this post also:

EnableRaisingEvents (enabling and disabling it)

try
{
    watcher.EnableRaisingEvents = false;
    //Edit2: Remove the watcher event
    watcher.Changed -= new FileSystemEventHandler(convert);

    try
    {
      XmlDocument xdoc = new XmlDocument();
      xdoc.Load(FileName);
    }
    catch (XmlException xe)
    {
      System.Threading.Thread.Sleep(1000);  //added this line
      using (StreamWriter w = File.AppendText(FileName))
      {
        Console.WriteLine(xe);
        w.WriteLine("</test>");
        w.WriteLine("</testwrapper>");
      }
    }
}

/*
   Here all xslt transform code
*/

    //Edit2: Add again the watcher event
    watcher.Changed += new FileSystemEventHandler(convert);
}
catch (Exception e)
{
   Console.WriteLine("The process failed: {0}", e.ToString());
}

7 Comments

-1 How or why is the file open. What should be done to fix it? The question wasn't answered.
There was another answer published near same time and a thread going on. I had not spent time in detailing my answer, so maybe I'll not get any +1, but the answer is right: "there is a lock in the file". The question was not "How or Why the file is open"
@Draykos: Thanks buddy, just praying you get more +1's. But we haven't yet reached a conclusion to this problem. Yes we now the file is locked but what can be done to solve this. I have tried various sorts of things but to no avail. Let me know if you come across something. Thanks
Yes, done that. But I still get the same exception: The process cannot access the file.This code is part of the convert(). If convert is fired it would first execute this code and then go forward to apply the xsl. The first time it finds a file it doesn't give me that exception. It processes it successfully. The second time it finds a file it gives me an error straight meaning. Meaning it process the catch (the process has failed) straight away for all files it finds. Don't know why ?
Try to remove event handler while you are doing stuff. See edited answer.
|
0

The solution to this problem is right at this link:

Exception during xml processing

This was another question I raised. Thank you all of you who spent their time in helping me out.

Comments

0

Make sure the file does not exist.

I had to recreate my build configuration and the old file still existed. Once I deleted the old transform I was able to recreate the new transform.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.