删除现有Xml文件时出错 [英] Error While deleting Existing Xml File

查看:131
本文介绍了删除现有Xml文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Xml文件.首先创建Xml时,我将检查文件是否已存在.如果存在,我正在尝试将其删除.删除现有文件时,我会收到类似以下错误:

该进程无法访问文件" C:\ Documents and Settings \ Administrator \ My Documents \ Visual Studio 2005 \ Projects \ XmlExercise \ XmlExercise \ bin \ Debug \ PurchaseBill.xml""


请帮忙.
在此先感谢

解决方案

此错误仅表示您的应用程序或任何其他应用程序已经打开了该文件.您想检查一下是否真的关闭了此文件的所有句柄,然后再尝试将其删除.如果您向我们展示一些代码,我们也许可以为您提供更好的建议.

评论后更新答案:

创建文件后,您正尝试删除该文件.您说过要检查它是否已经存在,然后将其删除.所以,这就是它的完成方式:

string path = Application.StartupPath + "\\PurchaseBill.xml";
if (File.Exists(path))
  File.Delete(path);

using(XmlWriter writer = XmlWriter.Create(path))
{
  writer = XmlWriter.Create(path); writer.WriteStartElement("Bill");   
  writer.WriteElementString("VoucherNo", dtHeaderInfo.Rows[0][0].ToString()); 
  writer.WriteElementString("VoucherType", dtHeaderInfo.Rows[0][1].ToString()); 
  writer.WriteElementString("VoucherDate", dtHeaderInfo.Rows[0][2].ToString()); 
  writer.WriteElementString("PartyId", dtHeaderInfo.Rows[0][3].ToString()); 
  writer.WriteElementString("BillNo", dtHeaderInfo.Rows[0][4].ToString());
  writer.WriteElementString("BillDate", dtHeaderInfo.Rows[0][5].ToString()); 
  writer.WriteEndElement(); 
  writer.Flush(); 
}


好吧,这显然是安全特权问题.确保提供了正确的访问安全性.可能需要为ASPNET授予完全删除权限.

您可以首先授予所有人对该文件的完全权限,然后检查您要使用的用户以删除该文件.在您发布的代码示例中,您有

write.Flush();



刷新缓冲区但不关闭文件.您需要添加一行以显式关闭文件

write.Close();



尝试删除它之前.


I am creating a Xml file. While creating the Xml firstly I will check if the file already exists. If it exists I am trying to delete it. While deleting the existing file I will get error like:

"The process cannot access the file ''C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XmlExercise\XmlExercise\bin\Debug\PurchaseBill.xml'' "


Please Help.
Thanks in advance

解决方案

This error simply means that your application or any other application has already opened the file. You would want to check if you really closed all handles of this file before trying to delete it. If you would show us some code we might be able to give you a better advice.

Updated answer after comment:

You are trying to delete the file after you created it. You said you want to check if it already exists and then delete it. So, this is how it''s done:

string path = Application.StartupPath + "\\PurchaseBill.xml";
if (File.Exists(path))
  File.Delete(path);

using(XmlWriter writer = XmlWriter.Create(path))
{
  writer = XmlWriter.Create(path); writer.WriteStartElement("Bill");   
  writer.WriteElementString("VoucherNo", dtHeaderInfo.Rows[0][0].ToString()); 
  writer.WriteElementString("VoucherType", dtHeaderInfo.Rows[0][1].ToString()); 
  writer.WriteElementString("VoucherDate", dtHeaderInfo.Rows[0][2].ToString()); 
  writer.WriteElementString("PartyId", dtHeaderInfo.Rows[0][3].ToString()); 
  writer.WriteElementString("BillNo", dtHeaderInfo.Rows[0][4].ToString());
  writer.WriteElementString("BillDate", dtHeaderInfo.Rows[0][5].ToString()); 
  writer.WriteEndElement(); 
  writer.Flush(); 
}


Well, it''s clearly security privilege issue. Make sure proper access securities are provided. Probably ASPNET needs to be given full permission to delete it.

You can start with giving Everyone as full permission to that file and check for the user that you are using in order to delete it.


I think the problem is simpler than the previous answer suggests. In the code example you posted, you have

write.Flush();



which flushes the buffers but does not close the file. You need to add a line to explicitly close the file

write.Close();



before trying to delete it.


这篇关于删除现有Xml文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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