windows 使用文件识别进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/379689/
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
Identify process using a file
提问by Amy
I have been trying to figure out how to programmatically identify the process that has a lock on a particular file. I've searched through the Win32 API and WMI, but so far I can't find anything. I know it's possible - Sysinternals is able to list every resource accessed/locked by every process on the system.
我一直在试图弄清楚如何以编程方式识别锁定特定文件的进程。我已经搜索了 Win32 API 和 WMI,但到目前为止我找不到任何东西。我知道这是可能的 - Sysinternals 能够列出系统上每个进程访问/锁定的每个资源。
Can anyone drop me a hint?
任何人都可以给我一个提示吗?
采纳答案by thejoshwolfe
You can use handle.exe from Sysinternals.
您可以使用Sysinternals 中的 handle.exe。
Something like:
就像是:
> handle /accepteula C:\path\to\directory
...
program.exe pid: 1234 type: File 2E4: C:\path\to\directory
...
Thanks to https://stackoverflow.com/a/599268/367916.
回答by jtomaszk
You could use Process Explorerfrom Microsoft
您可以使用Microsoft 的Process Explorer
- Download & unpack & run Process Explorer
- Click Findmenu and then click Find Handle or DLL...or press CTRL + F
- Copy and paste path to locked folder of file
- Click Search, you can kill process from main Process Explorerwindow
- 下载并解压并运行Process Explorer
- 单击查找菜单,然后单击查找句柄或 DLL...或按CTRL + F
- 将路径复制并粘贴到文件的锁定文件夹
- 单击Search,您可以从Process Explorer主窗口中终止进程
回答by Neil
If you can limit yourself to new enough versions of Windows, the Restart Manager can tell you which process has a particular file open.
如果您可以限制自己使用足够新的 Windows 版本,则重新启动管理器可以告诉您哪个进程打开了特定文件。
回答by chaos
Because of the way Process Explorer works, I suspect that what you need to look for is a way of finding the file handles attached to a given process, and that you'll have to pull that list for each process in the system and look for your file within it.
由于 Process Explorer 的工作方式,我怀疑您需要查找的是一种查找附加到给定进程的文件句柄的方法,并且您必须为系统中的每个进程提取该列表并查找你的文件在里面。
回答by Marc
I don't know in Windows, but somebody might find useful to know that, in Linux, you can use the lsofcommand, or just search through the folders /proc/PROCESS_ID/fd and see what process has opened the file.
我不知道在 Windows 中,但有人可能会发现知道,在 Linux 中,您可以使用lsof命令,或者只是搜索文件夹 /proc/PROCESS_ID/fd 并查看哪个进程打开了文件,这很有用。