views:

1115

answers:

3

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, with out closing that file.

thanks in advance, vijay

+2  A: 

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)

Adam Rosenfield
A: 

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.

David Dibben