Delete a file in use in runtime

后端 未结 4 1727
南旧
南旧 2021-01-23 11:31

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

相关标签:
4条回答
  • 2021-01-23 11:39

    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.

    0 讨论(0)
  • 2021-01-23 11:47

    See delete locked files, Overcoming “It is being used by another person or program.” among others.

    0 讨论(0)
  • 2021-01-23 11:50

    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)

    0 讨论(0)
  • 2021-01-23 11:53

    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
    
    0 讨论(0)
提交回复
热议问题