简体   繁体   English

WMI VBScript与C#

[英]WMI VBScript vs C#

I'm trying to get info about installed software on local computers (one is Windows 7 and other XP SP3) and I'm able to do it with VBScript, but not with C#. 我正在尝试获取有关本地计算机(一个是Windows 7和其他XP SP3)上已安装软件的信息,并且能够使用VBScript来实现,但不能使用C#。

Here is the VBScript code: 这是VBScript代码:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery _
    ("Select * from Win32_Product")

For Each objSoftware in colSoftware
    Wscript.Echo "Name: " & objSoftware.Name
    Wscript.Echo "Version: " & objSoftware.Version
Next

and here is the C# code: 这是C#代码:

string queryProd = "SELECT * FROM Win32_Product";                
ObjectQuery oQuery = new ObjectQuery(queryProd);
ManagementObjectSearcher searcherProd = new ManagementObjectSearcher(oQuery);
ManagementObjectCollection resultCollection = searcherProd.Get();

foreach (ManagementObject prodVar in resultCollection)
{
    Console.WriteLine("Product Name: {0}, Version: {1}.",
        (prodVar["Name"] == null) ? prodVar["Name"] : "/",
        (prodVar["Version"] == null) ? prodVar["Version"] : "/");
}

The second code snippet (C#) is not working. 第二个代码段(C#)无法正常工作。 It doesn't give me any error, it just returns null. 它没有给我任何错误,只是返回null。 The thing is that C# code works flawlessly when I'm using some other WMI class, such as Win32_ComputerSystem, for example. 问题是,当我使用其他WMI类(例如Win32_ComputerSystem)时,C#代码可以完美地工作。 But again, it's not working for Win32_DiskDrive class, also this particular case, etc. 但同样,它不适用于Win32_DiskDrive类,也不适用于这种情况。

In conclusion, in C#, WMI works only for some classes and in VBScript they all work. 总之,在C#中,WMI仅适用于某些类,而在VBScript中,它们均适用。 So, I'm wondering why is that? 所以,我想知道为什么吗?

Thanks for the answers. 感谢您的回答。

This is some code I generated using Microsoft's WMI code generator. 这是我使用Microsoft的WMI代码生成器生成的一些代码。 It seems to get the data you're after. 似乎可以获取您想要的数据。

You can download the generator here 您可以在此处下载生成器

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_Product"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Product instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("Version: {0}", queryObj["Version"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

Another C# code that is working in my case is the following: 在我的情况下,另一个有效的C#代码如下:

ManagementClass mgmt = new ManagementClass("Win32_Product");
ManagementObjectCollection objCol = mgmt.GetInstances();
foreach (ManagementObject obj in objCol)
{
    Console.WriteLine("Product Name: {0}, Version: {1}.",
        obj.Properties["Name"].Value.ToString(),
        obj.Properties["Version"].Value.ToString());                    
}

Hope that it will help someone. 希望它能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM