c# Exception 进程无法访问文件

use*_*720 5 .net c# ioexception

我遇到异常:该进程无法访问该文件。

这是代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

在我添加这些行之前,代码运行得很好。这些主要用于测试 xml 文件是否没有结束标签(我通常会获取然后添加标签)。添加以下代码后,它开始给我这个异常。

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>");
    }
}             
Run Code Online (Sandbox Code Playgroud)

这里可能出了什么问题?

编辑:我收到错误

进程失败:System.IO.IOException:进程无法访问文件“z:\TF_B1BBA.xml”,因为它正在被另一个进程使用。在System.IO.__Error.WinIOError(Int32 errorCode,String MaybeFullPath)在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,I nt32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项, SECURITY_ATTRIBUTES secAttrs、字符串 msgPath、布尔值 bFromProxy、布尔值 useLongPath) 在 System.IO.FileStream..ctor(字符串路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 bufferSize) 在 System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials 凭证、IWebProxy 代理、RequestCachePolicy (cachePolicy) 在 System.Xml.XmlUrlResolver.GetEntity(Uri AbsoluteUri、字符串角色、Type ofO bjectToReturn) 在 System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) 在 System.Threading.CompressedStack.runTryCode(Object) userData)在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode backoutCode,对象userData)在System.Threading.CompressedStack.Run(CompressedStack压缩Stack,Cont extCallback回调,对象状态)在System.Xml.XmlTextReaderImpl.OpenUrl () 在 System.Xml.XmlTextReaderImpl.Read() 处。在 System.Xml.XmlLoader.Load(XmlDocument doc,XmlReader reader,Boolean prese veWhitespace) 在 System.Xml.XmlDocument.Load(XmlReader reader) 在 System.Xml.XmlDocument.Load(字符串文件名) 在 GSelInterface.Program.convert(对象源,FileSystemEventArgs f) 位于 C:\ Documents and Settings\Administrator\Desktop\ConsoleApplication1\ConsoleApplication1\Program.cs:第 178 行

use*_*720 0

这个问题的解决方案就在这个链接:

xml处理过程中出现异常

这是我提出的另一个问题。感谢所有花时间帮助我的人。