Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileStream delete temp file automatically?

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.

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.

I've prefer case 1, but not sure if it is possible.

The source code is like following:

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();
    }
}

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:

There was an error opening this document. This file is already open or in use by another application.

The FileOptions.DeleteOnClose didn't work on purpose.... I want the temp file to be deleted when the Process application has been closed.

like image 373
Grmihel Avatar asked Feb 05 '26 08:02

Grmihel


1 Answers

Well, you have two issues:

  1. You need to close your stream before launching the external process. That is why you are getting the "file in use" error from Acrobat.
  2. After doing #1, Process.WaitForExit() should work as you would expect

    public void WriteFile(string filePath, StoredFile file, bool tempLocation)
    {
     // [snip..]
        try
        {
             fileStream.Write(data, 0, data.Length);
             fileStream.Flush();
             fileStream.Close();
    
             if (tempLocation)
             {
                Process p = System.Diagnostics.Process.Start(@strPath);
                p.WaitForExit();
                File.Delete(strPath);
             }
        }
        finally
        {
            if (fileStream != null)
              fileStream.Dispose();   
        }
    }
    
like image 51
Mike Marshall Avatar answered Feb 07 '26 21:02

Mike Marshall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!