简体   繁体   English

Windows:覆盖正在使用的文件

[英]Windows: Overwrite File In Use

I am trying to write a utility that will allow moving files in Windows, and when it finds a file in use, will set that file to be moved on reboot.我正在尝试编写一个实用程序,该实用程序将允许在 Windows 中移动文件,并且当它找到正在使用的文件时,将设置该文件以在重新启动时移动。

It seems that MoveFileEx ( http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx ) is the right call for this, however I cannot figure out what error code I'm looking for from GetLastError ( http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx ) to see that the file was in use.似乎 MoveFileEx ( http://msdn.microsoft.com/en-us/library/aa365240(VS.85).aspx )是正确的调用,但是我无法弄清楚我在寻找什么错误代码从 GetLastError ( http://msdn.microsoft.com/en-us/library/ms679360(VS.85).aspx ) 可以看到该文件正在使用中。

I want the utility to fail when there is an actual permissions problem.当存在实际权限问题时,我希望该实用程序失败。 Is there anyway to differentiate a you-can't-write-there and a in-use overwrite error?有没有办法区分你不能在那里写和正在使用的覆盖错误?

Also, if I have the files I am moving in the user's temporary folder, will they get deleted before the delayed rename?另外,如果我在用户的临时文件夹中有要移动的文件,它们会在延迟重命名之前被删除吗?

You have to call CreateFile first to see if the file is in use.您必须先调用CreateFile以查看文件是否正在使用中。

To see if the file is in use:要查看文件是否正在使用中:

If you get a valid file handle then you know the file does not have conflicting sharing permissions with a process that already has this file open.如果您获得有效的文件句柄,那么您就知道该文件与已经打开此文件的进程没有冲突的共享权限。

If you specify no sharing access (0 to the dwShareMode parameter of the CreateFile call), then you will not get a file handle if any other process is currently using that file in any way.如果您指定无共享访问权限(CreateFile 调用的 dwShareMode 参数为 0),那么如果任何其他进程当前正在以任何方式使用该文件,您将不会获得文件句柄。 GetLastError in this case would return: ERROR_SHARING_VIOLATION (32)在这种情况下,GetLastError 将返回: ERROR_SHARING_VIOLATION (32)


To see if there is a security problem with accessing the file:要查看访问文件是否存在安全问题:

To see if there is a permissions problem accessing that file, the CreateFile call will also fail but with a different GetLastError.要查看访问该文件是否存在权限问题,CreateFile 调用也会失败,但会出现不同的 GetLastError。 You will get: ERROR_ACCESS_DENIED (5)你会得到: ERROR_ACCESS_DENIED (5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM