title: File.WriteAllText not flushing data to disk
body: <p>I've had 3 reports now of user's machines crashing while using my software.. the crashes are not related to my program but when they restart the config files my program writes are all corrupt.</p>
  
  <p>There is nothing special to how the files are being written, simply creating a Json representation and dumping it to disk using File.WriteAllText()</p>
  
  <pre><code>// save our contents to the disk
  string json = JsonConvert.SerializeObject(objectInfo, Formatting.Indented);
  
  // write the contents
  File.WriteAllText(path, json);
  </code></pre>
  
  <p>I've had a user send me one of the files and the length looks about right (~3kb) but the contents are all 0x00.</p>
  
  <p>According to the post below File.WriteAllText should close the file handle, flushing any unwritten contents to the disk:</p>
  
  <p><a href="https://stackoverflow.com/questions/19710210/in-my-c-sharp-code-does-the-computer-wait-until-output-is-complete-before-moving">In my C# code does the computer wait until output is complete before moving on?</a></p>
  
  <p>BUT, as pointed out by Alberto in the comments:</p>
  
  <blockquote>
    <p>System.IO.File.WriteAllText when completes, will flush all the text to
    the filesystem cache, then, it will be lazily written to the drive.</p>
  </blockquote>
  
  <p>So I presume what is happening here is that the file is being cleared and initialized with 0x00 but the data is not yet written when the system crashes.</p>
  
  <p>I was thinking of maybe using some sort of temp file so the process would be like this:</p>
  
  <ol>
  <li>Write new contents to temp file</li>
  <li>Delete original file</li>
  <li>Rename temp file to original</li>
  </ol>
  
  <p>I don't think that will solve the problem as I presume Windows will just move the file even though the IO is still pending.</p>
  
  <p><strong>Is there any way I can force the machine to dump that data to disk instead of it deciding when to do it or perhaps a better way to update a file?</strong></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Based on suggestions by @usr, @mikez and @llya luzyanin I've created a new WriteAllText function that performs the write using the following logic:</p>
  
  <ol>
  <li>Create a temp file with the new contents using the FileOptions.WriteThrough flag</li>
  <li>Writes the data to disk (won't return until the write has completed)</li>
  <li>File.Replace to copy the contents of the new temp file to the real file, making a backup</li>
  </ol>
  
  <p>With that logic, if the final file fails to load, my code an check for a backup file and load that instead</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static void WriteAllTextWithBackup(string path, string contents)
  {
      // generate a temp filename
      var tempPath = Path.GetTempFileName();
  
      // create the backup name
      var backup = path + ".backup";
  
      // delete any existing backups
      if (File.Exists(backup))
          File.Delete(backup);
  
      // get the bytes
      var data = Encoding.UTF8.GetBytes(contents);
  
      // write the data to a temp file
      using (var tempFile = File.Create(tempPath, 4096, FileOptions.WriteThrough))
          tempFile.Write(data, 0, data.Length);
  
      // replace the contents
      File.Replace(tempPath, path, backup);
  }
  </code></pre>