In my VB.net application I am opening a PDF file using
System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf").
I would like to safely delete this file in some event if it is not open.
In my VB.net application I am opening a PDF file using
System.Diagnostics.Process.Start("c:\TEMP\MyFile.pdf").
I would like to safely delete this file in some event if it is not open.
只需尝试删除文件:
System.IO.File.Delete("THEFILE")
如果文件打开了,这行代码会抛出异常。Try
您可以(并且应该)通过用andCatch
块包装它来处理这种情况。例如:
Try
' Attempt to delete the file. This will succeed unless the file is in use.
System.IO.File.Delete("THEFILE")
Catch ex As IOException
' The file was in use, so it cannot be deleted.
' Do something here...or nothing if you just want to ignore such a case.
End Try
如果文件未打开,您的问题是寻求帮助删除文件。即使他们这样做,您似乎也不喜欢给出的响应。
是否仅在不使用文件时才删除该文件?在这种情况下,您执行已提到的操作,您使用Try... Catch
. 如果它抛出异常,那么它正在使用中 - 你不想删除它,否则你删除它。
但是,您给出的答复使您似乎无论如何都想删除它。请记住,在您将其加载到 vb 后,它被归类为正在使用,当您声称已使用完毕时,您需要将其丢弃。它不会在每次空闲时都被处理掉。如果您想使用它,请使用该.Dispose
方法或将持有它的任何内容设置为Nothing
.