Delete file after viewing?

  • Thread starter Thread starter Jon Brunson
  • Start date Start date
J

Jon Brunson

Hello all, and Happy New Year!

I'm trying to delete a file after displaying it to the user, I've got
the code below so far, but it deletes the file straight away, because
the process doesn't hold onto the file, or doesn't execute quick enough
to get a hold (sometimes the executed application reports "file not
found", other times they just hang).

I can't rely on prcPreview.WaitForExit(), or prcPreview.HasExited,
because programs like PocketWord don't actually exit when you close the
file, they just SmartMinimize (ppc programmers worst enemy)

Any ideas how to delete a file after it's been viewed?


[VB.NET] ("b" is a byte array containing the contents of the file)

Dim Filename As String = Path.GetTempPath() + Me.Description + "." +
Me.FileExtention
Dim FileStream As New FileStream(Filename, FileMode.Create)
FileStream.Write(b, 0, b.Length)
FileStream.Close()

Dim prcPreview As New OpenNETCF.Diagnostics.Process
prcPreview.StartInfo.FileName = Filename
prcPreview.StartInfo.UseShellExecute = True
prcPreview.Start()

Dim bFileInUse As Boolean = True
Dim tmp As FileStream = Nothing
While bFileInUse
Try
tmp = New FileStream(Filename, FileMode.Open, FileAccess.Read,
FileShare.None)
bFileInUse = False
Catch
End Try
If Not (tmp Is Nothing) Then tmp.Close()
System.Windows.Forms.Application.DoEvents()
End While

File.Delete(Filename)

[/VB.NET]
 
It's impossible to know the fact that the file is read or not by other
program but you can use small delay and after that delete your file.
Also you can check the FileSystemInfo.LastAccessTime to ensure that your
file was read after that wait for a certain time and delete.

Best regards,
Sergey Bogdanov
 
Back
Top