How can I delete a file that is in use by another process?

前端 未结 6 573
野性不改
野性不改 2020-12-10 05:34

When I try to delete a file occurs the following exception:

The process cannot access the file \'\' because it is being used by another process.<

相关标签:
6条回答
  • 2020-12-10 06:14

    Just rename this file. This will do the thing for whoever tries to write to that location.

    Notes:

    1) Of course the file is not deleted physically yet. Nice to do the MoveFileEx trick mentioned here around to complete the job.

    2) If you want to delete a locked file to write smth new in its place (e.g. during build), just rename the file to a GUID name. If you need the folder to be clean, either use an ignored extension / hidden attribute, or rename the file to a path under %TEMP% (if on the same drive).

    3) Not all locked files can be renamed, but it works for me for like 90% practical applications. You can move a file without affecting an open read/write/execute handle, it will continue working with the moved file just good (if moved within the same NTFS volume of course).

    4) That's what Windows Installer would basically do before it asks you to please reboot somewhen soon: move the file away from your eyes, schedule to be removed upon reboot. Usually the newly-installed app can be used right away.

    Practical Use:

    My favorite is with MSBuild. Overriding the <Copy/> task with this stuff makes all the build go linux-way. You don't care if a prev version is still running somewhere, can still build&run. The old app keeps using the old version of the files. The new app loads the newly-written version.

    Might be moving to %TEMP% if on the same drive (not my case though). I'd just rename them to an extension which is ignored with the current source control client.

    0 讨论(0)
  • 2020-12-10 06:15

    You can P/Invoke the Windows MoveFileEx function, and use the MOVEFILE_DELAY_UNTIL_REBOOT flag, with a NULL destination name. This will delete the file when you reboot.

    0 讨论(0)
  • 2020-12-10 06:22

    There is no way to delete a file that's currently being used by another process. You have to close whatever program has that file open first, before you can delete it.

    If you don't already know which program that is, you can figure it out using Handle or Process Explorer.

    0 讨论(0)
  • 2020-12-10 06:27

    slightly off topic: But it seems from your code that you are trying to delete all files of your folder. Well instead of deleting them one by one we have another method Directory.Delete(path, True) which will delete the directory as contained in the string named path. Then you may recreate the directory if you want. But your problem may persist here too.

    0 讨论(0)
  • 2020-12-10 06:28

    If the file is being used you're out of luck in trying to delete it. I can't tell you based on your code what process might be using the file(s), but try looking here or here or here, or at any of the other questions that show up as related to this one for guidance regarding this issue, and by all means follow the guidance from @Cody Gray about using Process Explorer.

    0 讨论(0)
  • 2020-12-10 06:32

    Another way is to find all open handles to the file and close them forcibly.

    Works nice for you, bad for any apps which were using the file.

    Could try that in UI with SysInternals ProcessExplorer.

    0 讨论(0)
提交回复
热议问题