troubleshooting Question

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

Avatar of joshipcontrols
joshipcontrolsFlag for United States of America asked on
C#Microsoft Development
1 Comment1 Solution2410 ViewsLast Modified:
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);
            }
        }
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros