“the process cannot access the file because it is being used by another process” when trying to delete file

前端 未结 4 1200

when deleting files one by one error is generated as \"the process cannot access the file \' because it is being used by another process when trying to delete file\"

cod

4条回答
  •  佛祖请我去吃肉
    2021-01-21 19:10

    If the file is in use you cannot delete it. However, if you for some reason really want to delete it and you are unable to stop the process that is locking the file (like when uninstalling an application) you can schedule the file for deletion the next time the operating system is restarted. These scheduled deletions are performed before any process is able to lock the file.

    You have to use the MoveFileEx Windows API using a null new file name and the flag MOVEFILE_DELAY_UNTIL_REBOOT. How to do that from C# is explained in an answer to the Stack Overflow question “MoveFile” function in C# (Delete file after reboot) C#.

提交回复
热议问题