title: c# Exception The process cannot access the file
body: <p>I'm getting an exception: The process cannot access the file.</p>
  
  <p>Here's the code:</p>
  
  <pre><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("&lt;/test&gt;");
              w.WriteLine("&lt;/testwrapper&gt;");
          }
      }
      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();
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>try
  {
      XmlDocument xdoc = new XmlDocument();
      xdoc.Load(FileName);
      xdoc = null;
  
  }
  catch (XmlException xe)
  {
      using (StreamWriter w = File.AppendText(FileName))
      {
          Console.WriteLine(xe);
          w.WriteLine("&lt;/test&gt;");
          w.WriteLine("&lt;/testwrapper&gt;");
      }
  }             
  </code></pre>
  
  <p>What could be wrong here ?</p>
  
  <p><strong>EDIT: Error I'm getting</strong></p>
  
  <blockquote>
    <p>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</p>
  </blockquote>