public static WriteFileAtomic ( string filePath, byte contents, bool deleteOnError ) : void | ||
filePath | string | |
contents | byte | |
deleteOnError | bool | |
return | void |
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;
}
}