C#异常的进程无法访问该文件 [英] c# Exception The process cannot access the file
问题描述
我发现了一个异常:该进程无法访问文件
这里的code:
如果(!Monitor.TryEnter(lockObject))
返回;
尝试
{
watcher.EnableRaisingEvents = FALSE;
尝试
{
XmlDocument的XDOC =新的XmlDocument();
xdoc.Load(文件名);
XDOC = NULL;
}
赶上(XmlException XE)
{
使用(StreamWriter的W = File.AppendText(文件名))
{
Console.WriteLine(XE);
w.WriteLine(&所述; /试验>);
w.WriteLine(&所述; / testwrapper>中);
}
}
System.Threading.Thread.Sleep(2000);
XPathDocument中myXPathDoc =新的XPathDocument(新的StreamReader(文件名,System.Text.Encoding.GetEncoding(视窗1256)));
XslCompiledTransform myXslTrans =新XslCompiledTransform();
myXslTrans.Load(D:/GS/xsl/test.xsl);
XmlTextWriter的myWriter =新的XmlTextWriter(destinationFile,NULL);
myWriter.Formatting = Formatting.Indented;
myWriter.Indentation = 4;
myXslTrans.Transform(myXPathDoc,空,myWriter);
myWriter.Close();
}
赶上(例外五)
{
Console.WriteLine(进程失败:{0},e.ToString());
}
最后
{
Monitor.Exit(lockObject);
watcher.EnableRaisingEvents = TRUE;
所以GC.Collect();
GC.WaitForPendingFinalizers();
所以GC.Collect();
}
在code的工作完全之前,我添加了这些行。这些主要是测试如果XML文件是没有结束标记(这)我就会再添加标记。之后,我增加了以下code就开始给我这个异常。
尝试
{
XmlDocument的XDOC =新的XmlDocument();
xdoc.Load(文件名);
XDOC = NULL;
}
赶上(XmlException XE)
{
使用(StreamWriter的W = File.AppendText(文件名))
{
Console.WriteLine(XE);
w.WriteLine(&所述; /试验>);
w.WriteLine(&所述; / testwrapper>中);
}
}
什么在这里可能是错了吗?
编辑:错误我收到
该进程失败信息:System.IO.IOException:该进程无法访问该文件'Z :\ TF_B1BBA.xml',因为它正被另一个进程。 在System.IO .__ Error.WinIOError(的Int32错误code,字符串maybeFullPath) 在System.IO.FileStream.Init(字符串路径,的FileMode模式,FileAccess的访问,我 NT32权利,布尔useRights,文件共享份额,的Int32缓冲区大小,FileOptionsØ ptions,SECURITY_ATTRIBUTES secAttrs,串MSGPATH,布尔bFromProxy,Boolea ÑuseLongPath) 在System.IO.FileStream..ctor(字符串路径,的FileMode模式,FileAccess的访问, 文件共享共享的Int32缓冲区大小) 在System.Xml.XmlDownloadManager.GetStream(URI URI,ICredentials凭据, IWebProxy代理,RequestCachePolicy的CachePolicy) 在System.Xml.XmlUrlResolver.GetEntity(URI绝对URI,字符串的作用,类型OFO bjectToReturn) 在System.Xml.XmlTextReaderImpl.OpenUrlDelegate(对象的XmlResolver) 在System.Threading.Com pressedStack.runTry code(对象USERDATA) 在System.Runtime.CompilerServices.RuntimeHelpers.Execute codeWithGuaranteedCl eanup(试code code,清理code撤销code,对象USERDATA) 在System.Threading.Com pressedStack.Run(比较pressedStack COM pressedStack,续 extCallback回调,对象的状态) 在System.Xml.XmlTextReaderImpl.OpenUrl() 在System.Xml.XmlTextReaderImpl.Read() 。在System.Xml.XmlLoader.Load(XmlDocument的文档,XmlReader的读者,布尔preSE veWhitespace) 在System.Xml.XmlDocument.Load(XmlReader的读者) 在System.Xml.XmlDocument.Load(字符串文件名) 在GSelInterface.Program.convert(对象源,FileSystemEventArgs F)在C:\ 的Documents and Settings \ Administrator的\桌面\ ConsoleApplication1 \ ConsoleApplicat ion1 \的Program.cs:行178
要解决这个问题就在此链接:
<一个href="http://stackoverflow.com/questions/15833525/exception-during-xml-processing/15834920#15834920">Exception在XML处理
这是另外一个问题,我提出。谢谢大家谁花时间帮助我。
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
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.
这篇关于C#异常的进程无法访问该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!