使用handle.exe查询指定文件被占用的进程

微软提供的查询进程利器:handle.exe

下载地址:https://docs.microsoft.com/zh-cn/sysinternals/downloads/handle

下面代码示例查询占用xx.doc的进程:

复制代码
string fileName = @"E:\xx.doc";
Process handlePro = new Process();
handlePro.StartInfo.FileName = @"E:\handle.exe";
handlePro.StartInfo.Arguments = fileName + " /accepteula";
handlePro.StartInfo.UseShellExecute = false;
handlePro.StartInfo.RedirectStandardOutput = true;
handlePro.Start();
handlePro.WaitForExit();
string outputTool = handlePro.StandardOutput.ReadToEnd();

Regex reg = new Regex(@"(?<=pid:).*?(?=type:)");
foreach (Match match in reg.Matches(outputTool))
{
   int pid = int.Parse(match.Value.Trim());
   Process pro = Process.GetProcessById(pid);
   string pn = pro.ProcessName;
}
复制代码

获得进程后可以进行后续相关的操作,kill进程等。

posted @   yuejin  阅读(1143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示