1

I have using System.Management;

However, I keep getting this error:

The type or namespace name 'ManagementClass' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

I get that error twice.

I also get this error:

The type or namespace name 'ManagementObjectCollection' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)

Why does this happen?

If it helps, this is my code (completely taken from StackOverflow, but still the code I'm using)

private string identifier(string wmiClass, string wmiProperty)
    //Return a hardware identifier
    {
        string result = "";
        System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
        System.Management.ManagementObjectCollection moc = mc.GetInstances();
        foreach (System.Management.ManagementObject mo in moc)
        {

            //Only get the first one
            if (result == "")
            {
                try
                {
                    result = mo[wmiProperty].ToString();
                    break;
                }
                catch
                {
                }
            }

        }
        return result;
    }

    private void getButton_Click(object sender, EventArgs e)
    {
        string modelNo = identifier("Win32_DiskDrive", "Model");
        string manufatureID = identifier("Win32_DiskDrive", "Manufacturer");
        string signature = identifier("Win32_DiskDrive", "Signature");
        string totalHeads = identifier("Win32_DiskDrive", "TotalHeads");
    }
Darshana
  • 2,462
  • 6
  • 28
  • 54
Minicl55
  • 947
  • 5
  • 14
  • 21
  • possible duplicate of ['ManagementClass' does not exist in the namespace 'System.Management'](http://stackoverflow.com/questions/1798152/managementclass-does-not-exist-in-the-namespace-system-management) – Mark Hall May 30 '12 at 05:27
  • Nowhere does Visual Studio say that you don't have the right using. Quote: "are you missing an assembly reference?" – Botz3000 May 30 '12 at 06:20

1 Answers1

3

The problem is that the DLL is not referenced under Solution/{PROJECT}/References. Right click on References and choose "Add Assembly" (or whatever) and go to the .NET tab and find System.Management.

Just because the using statement is there does not mean the DLL is referenced.

A nice guide is over on MSDN.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74