title: FileStream delete temp file automatically?
body: <p>Ihave searched a little around, but couldn't find something that excatly solved my problem.
  I have some code, that FileStream varbinary from my Database, and make it into a file on the client machine, that can be viewed in the default application for the file type when double clicked, and downloaded to the client PC when download btn clicked.</p>
  
  <p>The issue is, that when the user double click an item in the Listview (eg. mydocument.docx), my code will give it a temp name, and store it in the temp directory on the client machine. But this file ain't being deleted again?! How do I get the temp file I just created to be automatically deleted again, in those cases:
  1. The user close the associated application (eg. Word for .docx), which afterwards will delete the temp file again.
  2. The user close the Winform window, which will delete the temp file.
  3. All temp files created by the program, will be deleted on next reboot.</p>
  
  <p>I've prefer case 1, but not sure if it is possible.</p>
  
  <p>The source code is like following:</p>
  
  <pre><code>public void WriteFile(string filePath, StoredFile file, bool tempLocation)
  {
      byte[] data = file.FilContent.ToArray();
      FileStream fileStream;
      string tempName = Path.GetRandomFileName(), strPath;
      if (tempLocation)
           strPath = String.Format(@"{0}{1}{2}", Path.GetTempPath(), tempName, file.FilExt);
      else
           strPath = String.Format(@"{0}{1}", filePath, file.FilExt);
      fileStream = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 512, FileOptions.DeleteOnClose);
  
      try
      {
           fileStream.Write(data, 0, data.Length);
           fileStream.Flush();
  
           if (tempLocation)
              System.Diagnostics.Process.Start(@strPath);
      }
      finally
      {
           fileStream.Close();
      }
  }
  </code></pre>
  
  <p>I have try'd a lot... I have also attempted to use the Process.WaitForExit() method, but when I use it, my PDF application gives me following error message:</p>
  
  <blockquote>
    <p>There was an error opening this document. This file is already open or in use by another application.</p>
  </blockquote>
  
  <p>The FileOptions.DeleteOnClose didn't work on purpose.... I want the temp file to be deleted when the Process application has been closed.</p>