Avatar of joshipcontrols
joshipcontrols
Flag for United States of America asked on

When using System.Management; I'm not able to use any of it's methods/objects except Instrumentation

When using System.Management; I'm not able to use any of it's methods/objects except Instrumentation.
If I try to use ManagementObjectSearcher I get an error.
Error 1 The type or namespace name 'ManagementObjectSearcher' could not be found (are you missing a using directive or an assembly reference?

I'm trying to get the following Code to compile so that I can tell if a laptop is docked or not.
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_SystemEnclosure"); 
 
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_SystemEnclosure instance");
                    Console.WriteLine("-----------------------------------");
 
                    if(queryObj["ChassisTypes"] == null)
                        Console.WriteLine("ChassisTypes: {0}", queryObj["ChassisTypes"]);
                    else
                    {
                        UInt16[] arrChassisTypes = (UInt16[])(queryObj["ChassisTypes"]);
                        foreach (UInt16 arrValue in arrChassisTypes)
                        {
                            Console.WriteLine("ChassisTypes: {0}", arrValue);
                        }
                    }
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

Open in new window

Microsoft DevelopmentC#

Avatar of undefined
Last Comment
Bob Learned

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy