Winforms should save some runtime information in an XML file. The file size can sometimes be several hundred kilobytes. During beta testing, we found that some users do not hesitate to terminate seemingly random processes and occasionally cause the file to be half written and therefore corrupted.
Thus, we changed the algorithm to save in a temporary file, and then to delete the real file and perform the move.
Our code currently looks like this.
private void Save()
{
XmlTextWriter streamWriter = null;
try
{
streamWriter = new XmlTextWriter(xmlTempFilePath, System.Text.Encoding.UTF8);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyCollection));
xmlSerializer.Serialize(streamWriter, myCollection);
if (streamWriter != null)
streamWriter.Close();
System.IO.File.Delete(xmlFilePath);
System.IO.File.Move(xmlTempFilePath, xmlFilePath);
}
catch (System.Exception ex)
{
throw new InvalidOperationException("Could not save the xml file.", ex);
}
finally
{
if (streamWriter != null)
streamWriter.Close();
}
}
. 12 , 5 . :
System.InvalidOperationException:
Could not save the xml file.
---> System.IO.IOException: Cannot create a file when that file already exists.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.Move(String sourceFileName, String destFileName)
at MyApp.MyNamespace.InternalSave()
Move.
Win7.
: - Flush() ? ,.net, - ? Thread.Sleep(x)? , File.Copy(src, dest, true)? ? ( .)
while (System.IO.File.Exists(xmlFilePath))
{
System.IO.File.Delete(xmlFilePath);
}
bool done = false;
while (!done)
{
try
{
System.IO.File.Move(xmlTempFilePath, xmlFilePath);
done = true;
}
catch (System.IO.IOException)
{
}
}
- ?