When trying to replace the content of an XML file in C#.NET with a snippet like this:
string file = Path.GetTempFileName(); // pretend this is a real file
string tmpFile = Path.GetTempFileName();
using (var writer = XmlWriter.Create(File.Create(tmpFile)))
{
writer.WriteStartElement("root");
for (int i = 0; i < 100; i++)
{
writer.WriteElementString("test", null,
"All work and no play makes Jack a dull boy");
}
writer.WriteEndElement();
}
File.Delete(file);
File.Move(tmpFile, file);
... I get a System.IO.IOException claiming that the file is already opened by another process.