释放 Windows 文件共享锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/320128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Releasing Windows file share locks
提问by Jonathan Wright
This problem crops up every now and then at work. Our build machine can have it's files accessed via a normal windows file share. If someone browses a folder remotely on the machine, and leaves the window open overnight, then the build fails (as it has done now). The explorer window left opened points at one of the sub folders in the source tree. The build deletes the source, and does a clean checkout before building. The delete is failing.
这个问题在工作中时不时地出现。我们的构建机器可以通过普通的 Windows 文件共享访问它的文件。如果有人远程浏览机器上的文件夹,并在一夜之间打开窗口,那么构建就会失败(就像现在所做的那样)。资源管理器窗口左打开指向源树中的子文件夹之一。构建删除源,并在构建之前进行干净的检出。删除失败。
Right now, I'd like to get the build to work. I'm logged in from home, and I'd rather not reboot the build machine. I'm unable to get hold of the person whose machine is looking and the files, and I can't remotely reboot their machine.
现在,我想让构建工作。我是从家里登录的,我不想重新启动构建机器。我无法找到正在查找机器的人和文件,也无法远程重启他们的机器。
When a windows share has a lock, the locking process is System, so I don't think I can kill it, as with normal locks.
当 Windows 共享有锁时,锁定进程是 System,所以我认为我不能像普通锁一样杀死它。
Does anyone know a way to release the lock on a shared folder without having to reboot the machine?
有谁知道一种无需重新启动机器即可释放共享文件夹锁定的方法?
采纳答案by Jonathan Wright
Found a solution.
找到了解决办法。
Find the process using Process Explorer:
- Download and extract procexp.exe
- In Process Explorer use the "Find Handle or DLL..." command from the "Find" menu
- Enter in the name of the directory which is having trouble deleting
- A list of open files which match that name should be shown. Take some guesses and find which one is failing to be deleted. If the file is locked by a windows share, the process holding the file will be System
- Note down the directory which was left open
Download and install the Unlocker (Warning: Link removed, as it contains malware)
- Install Unlocker, disabling the option for Explorer extensions and other junk
Unlock the directory
- Open up a cmd window, and navigate to C:\Program Files\Unlocker
- From the cmd window, run Unlocker.exe "the-path-to-the-locked-folder"
- A dialog will pop up confirming the lock release. Use the unlock button to unlock the file
使用Process Explorer查找进程:
- 下载并解压procexp.exe
- 在 Process Explorer 中,使用“Find”菜单中的“Find Handle or DLL...”命令
- 输入删除有问题的目录名
- 应显示与该名称匹配的打开文件列表。猜一猜,找出哪一个没有被删除。如果文件被 Windows 共享锁定,则持有该文件的进程将为 System
- 记下保持打开状态的目录
下载并安装解锁器(警告:链接已删除,因为它包含恶意软件)
- 安装 Unlocker,禁用 Explorer 扩展和其他垃圾选项
解锁目录
- 打开一个 cmd 窗口,然后导航到 C:\Program Files\Unlocker
- 从 cmd 窗口,运行 Unlocker.exe "the-path-to-the-locked-folder"
- 将弹出一个对话框,确认锁定释放。使用解锁按钮解锁文件
Now the directory should be unlocked, and can now be deleted.
现在目录应该被解锁,现在可以删除。
回答by sudheeshix
If you are admin on the server sharing the file over the network, you can use the Windows in-built feature:
如果您是通过网络共享文件的服务器上的管理员,则可以使用 Windows 内置功能:
- Start → My Computer → Right-click → Manage gets you to the Computer Management console
- In the left nav, navigate to Systems Tools → Shared Folders
- You can view Shares, Sessions & Open Files here. This allows you to find out who has opened which files from which workstations.
- Right-click on an item in the list to be able to remove the file lock.
- 开始 → 我的电脑 → 右键单击 → 管理让您进入计算机管理控制台
- 在左侧导航中,导航到系统工具 → 共享文件夹
- 您可以在此处查看共享、会话和打开的文件。这使您可以找出谁从哪些工作站打开了哪些文件。
- 右键单击列表中的项目可以解除文件锁定。
Hope this helps.
希望这可以帮助。
回答by Charles Burns
Try Process Hacker: https://wj32.org/processhacker/
尝试进程黑客:https: //wj32.org/processhacker/
Process hacker is like Process Explorer on steroids.
进程黑客就像类固醇上的进程资源管理器。
To findthe offending process, press CTRL+F or click the "Find Handles of DLLs" button and search for the file name.
要查找有问题的进程,请按 CTRL+F 或单击“查找 DLL 句柄”按钮并搜索文件名。
Once you find the file in the find handles dialog, you can simply right click the file there and choose "close". (at least for v2.39.124)
在查找句柄对话框中找到文件后,您只需右键单击该文件并选择“关闭”即可。(至少对于 v2.39.124)
Older versions had a "terminator" option in the context menu of the process.
Right click on the offending process --> Miscellaneous
--> Terminator
--> Select termination techniques. Note that some are possibly dangerous and may have unintended consequences.
旧版本在进程的上下文菜单中有一个“终止符”选项。右键单击违规进程 --> Miscellaneous
--> Terminator
-->选择终止技术。请注意,有些可能是危险的,可能会产生意想不到的后果。
回答by Cogent
The way i do it is by using both OpenFiles.exeand Handle.exeYou can run them in any order and you will have your resource fully unlocked.
我这样做的方法是同时使用OpenFiles.exe和Handle.exe您可以按任何顺序运行它们,并且您的资源将完全解锁。
OpenFiles: to disconnect File Sharing sessions
OpenFiles:断开文件共享会话
Handle.exe: to release any open handles (don't try to close handles belonging to pid4, since that's the system process)
Handle.exe:释放任何打开的句柄(不要试图关闭属于 pid4 的句柄,因为那是系统进程)
You can automate this by using powershell, batch, or any language of your choice.
您可以使用 powershell、批处理或您选择的任何语言来自动执行此操作。
回答by Sean
I've had similar problems, and none of these suggestions I've seen above look suitable for automated overnight builds (as the original poster implied) because they all require manual effort to hunt down and kill the locks.
我遇到过类似的问题,而且我上面看到的这些建议都不适合自动隔夜构建(正如原始海报所暗示的那样),因为它们都需要手动努力来寻找和杀死锁。
The only method I've tried that seems to work reliably is to remove the share itself, make the build, then add the share back. Here's one way of removing the share automatically:
我尝试过的似乎可靠的唯一方法是删除共享本身,进行构建,然后重新添加共享。这是自动删除共享的一种方法:
D:\Projects>net share Projects /DELETE /Y
D:\Projects>net share Projects /DELETE /Y
Users have open files on Projects. Continuing the operation will force the files closed.
用户在项目上打开了文件。继续操作将强制关闭文件。
Projects was deleted successfully.
项目已成功删除。
(NOTE: Creating the share again automatically can be a pain if the privilege groups you need to give it are messy.)
(注意:如果您需要授予它的权限组是混乱的,则再次自动创建共享可能会很痛苦。)
回答by Panuels
Another option is, starting from Windows Vista, to use the Windows tool built into the system:
另一种选择是,从 Windows Vista 开始,使用系统内置的 Windows 工具:
monitor resources: perfmon.exe /res
监控资源: perfmon.exe /res
Extracted from: Http://www.sysadmit.com/2017/06/windows-how-to-know-that-process-has-open-a-file.html
摘自:http: //www.sysadmit.com/2017/06/windows-how-to-know-that-process-has-open-a-file.html