处理错误,请帮助修复,也不确定为什么它不起作用 [英] Process Error, Please Help Fix, Nor Sure Why It Won't Work

查看:89
本文介绍了处理错误,请帮助修复,也不确定为什么它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,它会在编码部分出现进程错误



  if  new  FileInfo(loginDetails).Length ==  0 
{
XmlTextWriter xwriter = new XmlTextWriter(loginDetails,Encoding.UTF8);







请给我任何建议,多年来一直困扰着我。 PS loginDetails是文件路径的变量。

解决方案

这意味着XML文件被其他进程或应用程序使用。因此,首先看看Windows任务管理器,并尝试找到正在访问该应用程序的应用程序。结束该应用程序并再试一次。



如果您只是进行文件操作并且最后没有关闭或处理它,也会发生这种情况。

因此,请检查您的代码并尝试处理为XML操作打开的资源。


如果文件存在则不显示错误。仅在创建时文件



如果你看一下MSDN文档,那就很简单了: http://msdn.microsoft.com/en-us/library/d62kzs03(v = vs.110)的.aspx [< a href =http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

File.Create创建一个新文件,并返回一个打开的流,这样你就可以写信给它了



所以如果你调用File.Create然后尝试在同一个文件上创建一个XMLWriter,你将总是得到一个正在使用的文件错误 - 因为它正在被File.Create语句使用就在它上面! :笑:



相反,只需完全摆脱Exists检查和Create调用 - 当你创建一个新的XmlTextWriter时,它会自动截断或创建文件反正。

Using this code, it comes up with a process error with the encoding part

if (new FileInfo(loginDetails).Length == 0)
{
    XmlTextWriter xwriter = new XmlTextWriter(loginDetails, Encoding.UTF8);




any suggestions please, been bugging me for ages. PS the loginDetails is a variable for a file path.

解决方案

That means the XML file is used by other process or application. So, first of all take a look at the Windows Task Manager and try to locate which application is accessing that. End that application and try again.

This also occurs if you just do file operations and don't close or dispose that at last.
So, check your code and try to dispose the resources you have opened for XML operations.


"if the file exists it doesn't show an error. Only when it creates file"

It's pretty simple then, if you look at the MSDN documentation: http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx[^]
File.Create create a new file, and returns an open stream so you can write to it

So if you call File.Create then try to create an XMLWriter on the same file, you will always get a "file in use" error - because it is in use by the File.Create statement immediately above it! :laugh:

Instead, just get rid of the Exists check and the Create call completely - when you create a new XmlTextWriter, it truncates or creates the file for you automatically anyway.


这篇关于处理错误,请帮助修复,也不确定为什么它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆