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 applic开发者_开发问答ation has been closed.
Well, you have two issues:
- You need to close your stream before launching the external process. That is why you are getting the "file in use" error from Acrobat.
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(); } }
One solution would be to have a timer which periodically deletes all files in a predefined location.
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
精彩评论