WMI,Win32_Process

时间:2018-07-17 11:08:33

标签: c# wmi

我想使用 C#和WMI 查找哪些进程正在使用特定的dll。尝试了3种方法...

  1. 获取文件的CIM_DataFile对象,然后获取相关的进程。它适用于系统dll,但不适用于我的应用程序dll。即使进程资源管理器显示相同的内容,也不会显示与此相关的任何进程。 p>

  2. 枚举所有进程,然后枚举进程模块以查找DLL是否正在使用。这里也是同样的问题..适用于系统dll,但不适用于我的应用程序dll。

  3. 获取CIM_ProcessExecutables对象并获取DLLS / EXE及其链接的进程...这里也发生了同样的事情...

有什么我想念的吗?

在此处输入代码

using System;
using System.Management;
// Search the processes which have a certain file open
class App
{
    public static void Main()
    {
        WhoHasThisFileOpen("c:\\\\users\\\\pawarnit\\\\source\\\\repos\\\\myprogram\\\\myprogram\\\\bin\\\\debug\\\\differentDllFile.dll");
        Console.ReadLine();
    }
    static void WhoHasThisFileOpen(string objectQry)
    {
        SelectQuery query = new SelectQuery("select name from cim_datafile where name = '" + objectQry +"'" );
    using (ManagementObjectSearcher searcher = new
    ManagementObjectSearcher(query))
        {
            foreach (ManagementObject mo in searcher.Get())
            {
                Console.WriteLine("File Name: {0} \nIs currently opened by:",
                mo.Properties["Name"].Value);
                // Get processes having this File open
                foreach (ManagementBaseObject b in mo.GetRelated("Win32_Process"))
                {
                    ShowProcessProperties(b.ToString());
                    b.Dispose();
                }
            }
        }
    }
    static void ShowProcessProperties(string objectClass)
    {
        using (ManagementObject process = new ManagementObject(objectClass))
        {
            process.Get();
            PropertyDataCollection processProperties = process.Properties;
            Console.WriteLine("ProcessID: {0,6} \tCommandLine: {1}",
            processProperties["ProcessID"].Value,
            processProperties["CommandLine"].Value);
        }
    }
}

0 个答案:

没有答案