问题描述
我的应用程序中已经安装了Microsoft.Management.Infrastructure,至少在第一个示例代码中如此:
I've got Microsoft.Management.Infrastructure working in my application, at least as far as the very first sample code:
CimSession.Create(null)
.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_OperatingSystem")
.FirstOrDefault().CimInstanceProperties["Version"].Value.ToString();
我需要使用MMI来确定本地计算机上的任何应用程序是否都锁定了特定文件(请参阅,以了解为什么除了MMI之外的其他方法都无法正常工作
What I need is to use MMI to determine whether any applications on the local machine have a lock on a specific file (see this question to see why approaches besides MMI aren't working)
我一直在逐页阅读有关MMI,WQL和CIM以及大量其他TLA的文档,但是却不知道该怎么做
I've been reading page after page of documentation on MMI and WQL and CIM and a flock of other TLAs but cannot figure out how to either
1)问一个问题:哪个进程打开或锁定了文件X?
1) ask the question "which process has file X open/locked"?
或
2)枚举所有打开/锁定的文件,以便我可以查找文件X
2) enumerate all open/locked files so I can look for file X
重要-我需要在代码中执行此操作(运行Process Explorer对我不起作用).
Important - I need to do this in code (running Process Explorer won't work for me).
推荐答案
据我所知,在CIM/WMI中是不可能做到的.
As far as I know, it is not possible to do in CIM/WMI.
如果有时间,您可以使用类似 WMI资源管理器.或者,您可以通过在PowerShell中运行类似这样的内容来仅查看包含某些属性名称的类来限制搜索:
If you have time, you can inspect ~1400 available CIM/WMI classes in a tool like WMI Explorer. Or you can limit your search by looking only at the classes containing certain property names by running something like this in PowerShell:
Get-CimClass -PropertyName *handle*
其中handle
是您感兴趣的属性名称.
where handle
is a property name you're interested in.
您可能会认为 CIM_LogicalFile.InUseCount
提供类似于您所需的内容,但抱怨它不起作用可以追溯到2003年.很有可能从未实现过.
You could think that the CIM_LogicalFile.InUseCount
provides something similar to what you need but the complaints that it doesn't work go as back as 2003. It is quite possible that it was never implemented.
作为一个旁注,似乎大多数文件解锁器"工具的作者都在假设锁定文件意味着进程拥有文件句柄的情况下工作,因此您只需要枚举所有活动文件句柄并进行关联他们与正在运行的进程列表.不幸的是,WMI中没有允许您执行此操作的类,但是即使有这样的类,它也不适用于内存映射文件(根据您的另一个问题判断是您关心的是什么),因为大多数应用程序在打开内存映射文件后立即处理文件句柄.为了获取该信息,您需要枚举进程中的虚拟内存区域,然后查询Windows内存管理器,询问与该区域对应的图像或内存映射文件.这样的任务感觉远远超出了WMI通常能够执行的范围.
As a side note, it seems that most authors of "file unlocker" tools operate under assumption that a locked file means that there is a file handle owned by a process, so you just need to enumerate all active file handles and correlate them with the list of running processes. Unfortunately, there is no class in WMI that allows you to do that, but even if there was such class, it wouldn't work for the memory-mapped files (which judging by your other question is what you are concerned with) because most apps dispose the file handle as soon as they open the memory-mapped file. In order to get that information, you would need to enumerate virtual memory regions in a process and then query Windows memory manager asking what image or memory-mapped file corresponds to that region. A task like this feels quite far out of scope of what WMI is typically able to do.
这篇关于如何在应用程序中使用Microsoft.Management.Infrastructure来确定哪个应用程序已锁定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!