0

What will happen if application crashes during XmlDocument.Save() method?

For example, I am saving some huge inforamtion into Xml file. Application crashes and file's xml format becomes incorrect and it can't be loaded again. How to prevent it?

Also, what about the application's setting file? Will the validity be broken If app crashes during saving setting's file ?

HelloWorld
  • 1,061
  • 2
  • 15
  • 25

1 Answers1

0

If application crashes during saving settings file, than that file will not be valid anymore. For a small xml files I am using next code to prevent corruption:

Using ms As New MemoryStream
   xmlDoc.Save(ms)
   Using outStream As FileStream = File.Open(filename,
             FileMode.Create, FileAccess.Write, FileShare.Read)
      ms.WriteTo(outStream)
   End Using
End Using

For big files there are different ways to do that:

  1. There are transactional NTFS available under Vista and up, Atomic file copy under .NET, Atomicity of File.Move
  2. Use temporary file to save information, then use rename for safe replace of old file
Community
  • 1
  • 1
walter
  • 843
  • 2
  • 14
  • 35