public static void WriteFileAtomic(string filePath, byte[] contents, bool deleteOnError)
{
var tempPath = CreateTmpName(filePath);
try
{
CreateNonExistentDirectory(Path.GetDirectoryName(filePath));
File.WriteAllBytes(tempPath, contents);
DeleteFile(filePath);
File.Move(tempPath, filePath);
}
catch (Exception)
{
if (deleteOnError)
{
DeleteFileSilently(filePath);
}
DeleteFileSilently(tempPath);
throw;
}
}