ManagementObjectSearcher on WMI array property

I'm running the following code in attempt to get the ChassisType of the device it is run on.

string wmiQuery = string.Format(
    "SELECT ChassisTypes FROM Win32_SystemEnclosure");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject obj in searcher.Get())

When I run this, and try to convert it to string, it gives me an empty string. When I try and examine what the searcher.get() is returning in Autos. It gives me an evaluation time out and then an exception... If I just let it run it does actually return

I'm ignoring the evaluation because I get the same exception when i try and examine other WMI queries and I know they work and return the required values. I also dont know where to start with this, although it is hindering troubleshooting.

I think the problem is that the ChassisTypes property is an array... but I've no idea how to get the value out. I cant run another foreach on my "obj" managementobject, because managementobjects do have a GetEnumerator.

Any ideas on how to 1. sort the evaluation time out... or even better 2. access WMI array properties.

Cheers


Sorted 2. by googling correctly :P

I had to but the objects returned by the searcher into an array and then foreach through that array to get my value:

    var deviceType = "";
    string wmiQuery = string.Format(
        "SELECT ChassisTypes FROM Win32_SystemEnclosure");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
    ManagementObjectCollection qCollect = searcher.Get();
    foreach (ManagementObject obj in qCollect)
    {
        Int16[] rs = (Int16[])obj["ChassisTypes"];
        foreach (var item in rs)
        { 
            if (item == 9 ||
                item == 10 ||
                item == 14)
            {
                deviceType = "Laptop";
            }
            else
            {
                deviceType = "Non-Laptop";
            }

Still no idea why i get an exception when trying to view the searcher though. But my problem is sorted.

链接地址: http://www.djcxy.com/p/19200.html

上一篇: 获取ManagementObject属性的最快方法

下一篇: ManagementObjectSearcher在WMI数组属性上