简体   繁体   中英

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.

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

一种解决方案是具有定时器,其定期删除预定义位置中的所有文件。

I'd take another approach: either

  • Let the user decide the filename and location and you'll never have to delete it or
  • Use subfolder in the temp directory and empty it/recreate it on startup of the application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM