我研究了几个问题,但我找到的答案都没有帮助。此函数的目标是修改 xml 文件。我读取原始文件并将旧的东西和新的东西写入新文件。所有这一切都完美无缺。当我完成并需要删除旧文件并移动新文件时,问题就出现了。
收到的错误是 jnv_config.xml 正在被另一个进程(阅读器文件)使用。
删除 Close 和/或 Dispose 并不能解决问题。
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 窗体项目。