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

天大地大妈咪最大 提交于 2019-11-30 07:30:12

问题


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.

My code looks like:

string[] files = Directory.GetFiles(@"C:\SEDocumentConverter\SOURCE");
foreach (string file in files)
{               
   File.Delete(file);
}

How can I solve this problem?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/5232647/how-can-i-delete-a-file-that-is-in-use-by-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!