I have a c# console app that frequently and quickly writes maybe 1-2mb worth of data to a file (keeps overwriting same file). I then run the program in an infinite loop like this
@echo off
:while
(
C:\sync.exe
goto :while
)
and then the c# writes text like this
private static async Task WriteFile(string filePath, string text)
{
string base_path = AppDomain.CurrentDomain.BaseDirectory;
string file_path = Path.Combine(base_path, filePath);
using (StreamWriter outputFile = new StreamWriter(file_path))
{
await outputFile.WriteAsync(text);
}
}
But what I've noticed is that when I end the program, with ctrl+c, or stop the debugger, while it's in the middle of writing, it stops the writing and leave the file corrupted and with missing data.
Is there a way that I can ensure that if the program is stopped mid-write, it either undoes the change (it overwrites the previous file that was there) leaving the old one back again, or it somehow finishes writing (which will take less than a second)?