C#异常:进程无法访问文件 [英] C# Exception: The process cannot access the file
问题描述
我遇到一个例外:该进程无法访问该文件.
I'm getting an exception: The process cannot access the file.
这是我的代码中的一个片段:
Here's a snippet from my code:
在添加这些行之前,代码运行良好.这些主要用于测试xml文件是否没有结束标记(通常我会得到这些标记),然后添加标记.添加以下代码后,它开始为我提供此异常.
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.
尝试
{
XmlDocument xdoc =
新 XmlDocument ();
xdoc . 加载 ( FileName );
xdoc = 空 ;
}
catch ( XmlException xe )
{
使用 ( StreamWriter w
= 文件 . AppendText ( FileName ))
{
控制台 . WriteLine ( xe );
w . WriteLine ( </test>" );
w . WriteLine ( </testwrapper>" );
}
}
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.IO.IOException:该进程无法访问文件'z:\ TF_B1BBA.xml',因为该文件正在被另一个进程使用.在System.IO.FileStream.Init(字符串路径, FileMode模式,FileAccess访问权限,I nt32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolea和useLongPath)位于System.IO.FileStream..ctor(字符串路径, System.Xml.XmlDownloadManager.GetStream(Uri uri,ICredentials凭据,IWebProxy代理,RequestCachePolicy cachePolicy)位于System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri,字符串)的FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize) 角色,System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)的System.Threading.CompressedStack.runTryCode(Object userData)的类型,System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCl eanup(TryCode, System.Xml.XmlTextReaderImpl.OpenUrl()处System.Xml.XmlTextReaderImpl.Read()处的CleanupCode backoutCode,对象userData)在System.Xml.XmlTextReaderImpl.OpenUrl()处.在System.Xml.XmlLoader.Load(XmlDocument doc,XmlReader阅读器,System.Xml.XmlDocument.Load(XmlReader阅读器),System.Xml.XmlDocument.Load(String文件名),GSelInterface.Program.convert(Object source,FileSystemEventArgs f)在C:\文件和设定\ Administrator \ Desktop \ ConsoleApplication1 \ ConsoleApplicat ion1 \ Program.cs:line 178
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
推荐答案
可能是在另一个线程中打开了该文件吗?甚至是另一个程序?
May be this file was opened in the other thread? Or even by another program?
这篇关于C#异常:进程无法访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!