在没有 FILE_SHARE_DELETE 的情况下重命名在另一个进程中打开的文件
Renaming a file open in another process without FILE_SHARE_DELETE
使用 Windows API,是否可以重命名在没有 FILE_SHARE_DELETE
权限的情况下在另一个进程中打开的文件?
我认为这是不可能的,因为 CreateFile
docs 说:
Note: Delete access allows both delete and rename operations.
与此一致,在最终用户命令提示符中通过常规rename
重命名,我经常遇到The process cannot access the file because it is being used by another process.
;通过最终用户 GUI 和 python 的 os.rename
.
重命名会发生类似的错误
然而,this 高度赞成的答案似乎恰恰相反:
Deleting will fail if any other process has the file opened without delete sharing, renaming is never a problem.
这不仅仅是一个小问题;整个答案取决于该声明:如果删除失败时重命名不会失败,它只会解决 OP 问题。
我错过了什么?
注意:如果重要的话,我指的是 Windows 10,使用 C++ API(我认为它比 C# API 更强大)。
编辑:将代码片段移至 new question。
它只能作为延迟操作,使用 MOVEFILE_DELAY_UNTIL_REBOOT
flag. It is a priviledged operation that requires the user context to enable the Administrator token (UAC)。这是安装程序在使用时替换文件的方式,然后他们要求操作员重新启动系统。
如您所见,这不是微不足道的,您应该尽量避免这样做。
使用 Windows API,是否可以重命名在没有 FILE_SHARE_DELETE
权限的情况下在另一个进程中打开的文件?
我认为这是不可能的,因为 CreateFile
docs 说:
Note: Delete access allows both delete and rename operations.
与此一致,在最终用户命令提示符中通过常规rename
重命名,我经常遇到The process cannot access the file because it is being used by another process.
;通过最终用户 GUI 和 python 的 os.rename
.
然而,this 高度赞成的答案似乎恰恰相反:
Deleting will fail if any other process has the file opened without delete sharing, renaming is never a problem.
这不仅仅是一个小问题;整个答案取决于该声明:如果删除失败时重命名不会失败,它只会解决 OP 问题。
我错过了什么?
注意:如果重要的话,我指的是 Windows 10,使用 C++ API(我认为它比 C# API 更强大)。
编辑:将代码片段移至 new question。
它只能作为延迟操作,使用 MOVEFILE_DELAY_UNTIL_REBOOT
flag. It is a priviledged operation that requires the user context to enable the Administrator token (UAC)。这是安装程序在使用时替换文件的方式,然后他们要求操作员重新启动系统。
如您所见,这不是微不足道的,您应该尽量避免这样做。