title: Secure usage of FileStreams
body: <p>I am writing an encryption program for fun. I stumbled across the problem, that a deleted file has not to be gone even if it is overwritten by opening it with a <code>FileStream</code> and writing a bunch of random bytes into it. </p>
  
  <p>But my current implementation of my program is creating a new temporary file, to write encrypted/decrypted data into it to save RAM. I was now wondering if the same problem does apply, even if I don't terminate the <code>FileStream</code> object, which exists since the files creation. </p>
  
  <p>So if I just set my stream position back to zero and overwrite every single byte, does it really write onto the same positions as in the beginning or can parts of the temp file survive? If so, is there any workaround I could use?</p>
  
  <p>My current approach:</p>
  
  <pre><code>var fileStream = new FileStream(path, FileMode.Create);
  fileStream.Write(//possible decrypted data);
  
  fileStream.Position = 0;
  
  byte[] bytes = RandomBytes();
  long amount = (fileStream.Length / bytes.Length + 1);
  
  for (long i = 0; i &lt; amount; i++)
  {
      fileStream.Write(bytes, 0, bytes.Length);
  }
  
  string name = fileStream.Name;
  fileStream.Close();
  File.Delete(name);
  </code></pre>