0

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();

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)

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) ask the question "which process has file X open/locked"?

or

2) enumerate all open/locked files so I can look for file X

Important - I need to do this in code (running Process Explorer won't work for me).

4

1 回答 1

1

据我所知,在 CIM/WMI 中是不可能的。

如果您有时间,您可以在WMI Explorer之类的工具中检查大约 1400 个可用的 CIM/WMI 类。或者,您可以通过在 PowerShell 中运行类似这样的操作,仅查看包含某些属性名称的类来限制搜索:

Get-CimClass -PropertyName *handle*

handle您感兴趣的属性名称在哪里。

您可能会认为它CIM_LogicalFile.InUseCount提供了与您需要的类似的东西,但是关于它不起作用的抱怨可以追溯到 2003 年。它很可能从未实施过。

作为旁注,似乎大多数“文件解锁器”工具的作者在假设锁定文件意味着有一个进程拥有的文件句柄的情况下进行操作,因此您只需枚举所有活动文件句柄并将它们与正在运行的进程列表。不幸的是,WMI 中没有允许您这样做的类,但即使有这样的类,它也不适用于内存映射文件(根据您的其他问题判断是您所关心的),因为大多数应用程序在打开内存映射文件后立即处理文件句柄。为了获取该信息,您需要枚举进程中的虚拟内存区域,然后查询 Windows 内存管理器,询问该区域对应的图像或内存映射文件。

于 2020-05-17T04:16:48.993 回答