gpt4 book ai didi

c# - 如何删除由 xmlreader 在 C# 中打开的文件?

转载 作者:行者123 更新时间:2023-11-30 21:00:49 29 4
gpt4 key购买 nike

我已经研究了几个问题,但我找到的答案都没有帮助。这个函数的目标是修改一个xml文件。我读取原始文件并将旧的东西和新的东西写入新文件。所有这一切都完美无缺。当我完成并需要删除旧文件并移动新文件时,问题就出现了。

收到的错误是 jnv_config.xml 正在被另一个进程(读取器文件)使用。

删除关闭和/或处置并不能解决问题。

using (XmlReader reader = XmlReader.Create("jnv_config.xml"))
using (XmlWriter writer = XmlWriter.Create("jnv_temp.xml"))
{
writer.WriteStartDocument();
while (reader.Read())
{
// Read the file, write to the other file - this part works perfectly.
// No filestreams nor anything else is created in here.
}
writer.WriteEndElement();
writer.WriteEndDocument();
reader.Close();
writer.Close();
reader.Dispose();
writer.Dispose();
}
// Delete the old file and copy the new one
File.Delete("jnv_config.xml");
//File.Move("jnv_temp.xml", "jnv_config.xml");

我正在使用 VS2012 (NET 4.5)、C#、标准 Windows 窗体项目。

最佳答案

您确定仍然打开文件的是这个 XmlReader 吗?您是否尝试过使用 Process Explorer确认在此代码执行之前配置文件没有打开的文件句柄?

关于c# - 如何删除由 xmlreader 在 C# 中打开的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718689/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com