我怎样才能找出为什么我的XML文件已被使用? [英] How can I find out why my XML file is already in use?

查看:142
本文介绍了我怎样才能找出为什么我的XML文件已被使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用下面的代码将XML文件写入硬盘驱动器时,就可以使用以下代码:

  XmlDocument doc = new XmlDocument(); 
doc.Load(D:\\project\\data.xml);
if(!Directory.Exists(D:\\project_elysian\\data\\\)+ System.DateTime.Today.ToString(dd-MM-yyyy)))
{
DirectoryInfo di = Directory.CreateDirectory(D:\\project_elysian\\data\\+ System.DateTime.Today.ToString(dd-MM-yyyy)) ;

XmlTextWriter writer = new XmlTextWriter(D:\\project_elysian\\\data\\+ System.DateTime.Today.ToString(dd-MM-yyyy) +\\+ System.DateTime.Now.ToString(HH-mm-ss)+.xml,null);
XmlTextWriter writerlatest = new XmlTextWriter(D:\\project\\data\\\\\\\\\\\\\\\\\\\\\\
writer.Formatting = Formatting.Indented;
writerlatest.Formatting = Formatting.Indented;
doc.Save(作家);
doc.Save(writerlatest);
doc = null;
writer.Flush();
writerlatest.Flush();

它根据需要写入XML文件,但之后当我尝试读取同一个XML文件asp.net页面(代码放置在一个C#代码隐藏文件中)使用下面的代码,它给出了一个错误

  string filename = d:\\project\\data\\latest\\today.xml; 

XmlSerializer serializer = new XmlSerializer(typeof(searchResult));
serializer.UnknownNode + = new XmlNodeEventHandler(serializer_UnknownNode);
serializer.UnknownAttribute + = new XmlAttributeEventHandler(serializer_UnknownAttribute);
FileStream fs = new FileStream(filename,FileMode.Open);

错误如下

 进程无法访问文件D:\ project \data\latest\today.xml,因为它正在被另一个进程使用。 

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,了解有关错误的更多信息以及源代码的位置。

异常详细信息:System.IO.IOException:进程无法访问文件D:\ project \data\latest\today.xml,因为它正在被另一个进程使用。

编辑:文件没有被其他进程使用


when I write the XML file to hard drive using the following code

    XmlDocument doc = new XmlDocument();
    doc.Load("D:\\project\\data.xml");
    if (!Directory.Exists("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy")))
    {
        DirectoryInfo di = Directory.CreateDirectory("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy"));
    }
    XmlTextWriter writer = new XmlTextWriter("D:\\project_elysian\\data\\" + System.DateTime.Today.ToString("dd-MM-yyyy") + "\\" + System.DateTime.Now.ToString("HH-mm-ss") + ".xml", null);
    XmlTextWriter writerlatest = new XmlTextWriter("D:\\project\\data\\latest\\today.xml", null);
    writer.Formatting = Formatting.Indented;
    writerlatest.Formatting = Formatting.Indented;
    doc.Save(writer);
    doc.Save(writerlatest);
    doc = null;
    writer.Flush();
    writerlatest.Flush();

it writes the XML file as desired but after that when I try to read that XML file in the same asp.net page (code placed in a C# Code Behind file) using the following code, it gives an error

    string filename = "D:\\project\\data\\latest\\today.xml";

    XmlSerializer serializer = new XmlSerializer(typeof(searchResult));
    serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
    serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
    FileStream fs = new FileStream(filename, FileMode.Open);

the error is as follows

The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: The process cannot access the file 'D:\project\data\latest\today.xml' because it is being used by another process.

EDIT: The file isn't being used by any other process

解决方案

Make sure you're closing your writer with a call like writer.Close();

这篇关于我怎样才能找出为什么我的XML文件已被使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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