-1
  public ManagementScope GetScope()
    {
        try
        {
            //string classScope="winmgmts:" + "{impersonationLevel=impersonate}!\\" + strComputer + "\\root\\cimv2";
            string serverString = @"root\cimv2";

            ManagementScope scope = new ManagementScope(serverString);


                ConnectionOptions options = new ConnectionOptions
                {
                    Impersonation = ImpersonationLevel.Impersonate,
                    Authentication = AuthenticationLevel.Connect,
                    EnablePrivileges = true
                };
                scope.Options = options; 

            return scope; 
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }
    }

    public void InvokeMethodsFunctions1()
    {
        ManagementScope mScope = GetScope();

        mScope.Connect();

        if (mScope.IsConnected)
        {
            ManagementClass processClass =
                new ManagementClass(mScope.Path);
            ManagementObjectSearcher mos = new ManagementObjectSearcher(mScope, new ObjectQuery("SELECT * FROM Win32_Product"));
            //get collection of WMI objects
            ManagementObjectCollection queryCollection = mos.Get();

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Result.txt"))
            {
                textBox1.Text = "";
                //enumerate the collection.
                foreach (ManagementObject m in queryCollection)
                {
                    // access properties of the WMI object
                    string line = " " + m["Name"] + " , InstallDate : " + m["InstallDate"] + " LocalPackage : " + m["LocalPackage"];
                    Console.WriteLine(line);
                    file.WriteLine(line);
                    textBox1.Text += line + "\n";
                }
            }

       }

    }

那么我的代码有什么问题?

4

2 回答 2

2

没有错,Win32_ProductWMI 类只列出了 Windows Installer (MSI) 安装的产品。

于 2013-10-30T16:50:38.220 回答
1

我刚刚测试了您的代码的以下简化版本,我看到所有东西都安装在我的电脑上,甚至包括我自己编写和安装的服务:

var products = new ManagementObjectSearcher(new ObjectQuery("SELECT * FROM Win32_Product"));
var result = products.Get();

foreach (var product in result)
{
    Console.WriteLine(product.GetPropertyValue("Name").ToString());
}

Console.ReadLine();

看起来您正在按范围缩小查询范围,这可能是您没有看到所有内容的原因,请尝试上述方法,看看您是否有更多的运气。

于 2013-10-30T16:01:53.770 回答