33 lines
908 B
C#
33 lines
908 B
C#
namespace Zcbot.WindowsNode;
|
|
|
|
internal static class AtomicFile
|
|
{
|
|
internal static void Write(string path, byte[] content, bool overwrite)
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
var temporaryPath = path + ".tmp-" + Guid.NewGuid().ToString("N");
|
|
try
|
|
{
|
|
using (var stream = new FileStream(
|
|
temporaryPath,
|
|
FileMode.CreateNew,
|
|
FileAccess.Write,
|
|
FileShare.None,
|
|
bufferSize: 4096,
|
|
FileOptions.WriteThrough))
|
|
{
|
|
stream.Write(content);
|
|
stream.Flush(flushToDisk: true);
|
|
}
|
|
File.Move(temporaryPath, path, overwrite);
|
|
}
|
|
finally
|
|
{
|
|
if (File.Exists(temporaryPath))
|
|
{
|
|
File.Delete(temporaryPath);
|
|
}
|
|
}
|
|
}
|
|
}
|