How to delete a file which is in use/open by some process in runtime. I am using vb.net for my project and a image is shown in picturebox, and that should be deleted, without cl
I don't think that it is possible. On windows, you cannot delete a file which has open handles. http://support.microsoft.com/kb/320081
However, from your description you don't need to keep the file open in your application. Open the file, read the image then close the file. Then the file can be deleted but the application will still have the image data.
See delete locked files, Overcoming “It is being used by another person or program.” among others.
If the file is opened by another process in exclusive mode, you can't -- Windows won't let you. In that case, the best you can do is to either wait for the other process to close the file and then delete it, or have it be deleted at the next reboot by using MoveFileEx() with the flag MOVEFILE_DELAY_UNTIL_REBOOT
and a destination location of NULL
.
If the file is opened non-exclusively by another process, you can just call DeleteFile() normally (assuming you have permission to do so). The file will remain while the other process has it open, but it will be deleted as soon as the other process closes it.
(And yes, I realize those links are for the Win32 C API; the same functions should be available under VB .NET)
You can close the proccess down, then delete it, or delete on reboot. Use the above suggestions for information on using MoveFileEx() with Windows.
To close down a process you can do the following:
For Each proc As Process In System.Diagnostics.Process.GetProcessesByName("process name here")
proc.Kill()
Next