System.IO.File.Delete () / System.IO.File.Move () sometimes does not work

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();

        // Delete the original file
        System.IO.File.Delete(xmlFilePath);

        // Do a move over the top of the original file 
        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);
}

// Do a move over the top of the main file 
bool done = false;
while (!done)
{
    try
    {
        System.IO.File.Move(xmlTempFilePath, xmlFilePath);
        done = true;
    }
    catch (System.IO.IOException)
    {
        // let it loop
    }
}

- ?

+5
4

Move.Copy true, , ?

App_Exit ?

+3

, . , , , , . .

, . .NET, FileShare.Delete. Windows , . " ". , File.Delete. , , . , .

, , , , File.Delete , File.Move . , File.Move, . , . , , , .

:

  • .new
  • .tmp
  • file.xml file.tmp
  • .new .xml
  • .tmp

5 .

+11

Save (, ), , , / , Move ( Move), Move.

anvarbek raupov, File.Copy(String, String, Boolean), ( ), - , , ( , , , ).

/ (, " ", , ).

+1

, , THEN .

NTFS, . github.com/haf/Castle.Transactions nuget... 2.5 .

.

3.0 is currently in pre-alpha state, but will integrate regular transactions with the io file with transactional files to a much higher level.

0
source

All Articles