17

Hi i'm using this method for get the mac address

public string GetMACAddress()
{
    System.Management.ManagementClass mc = default(System.Management.ManagementClass);
    ManagementObject mo = default(ManagementObject);
    mc = new ManagementClass("Win32_NetworkAdapterConfiguration");

    ManagementObjectCollection moc = mc.GetInstances();
    foreach (var mo in moc)
    {
        if (mo.Item("IPEnabled") == true)
        {
            return mo.Item("MacAddress").ToString();
        }else
            return null;

    }

} 

but i receive this error

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

What i have to do for fix it?

thanks

Matt
  • 22,721
  • 17
  • 71
  • 112
Luca Romagnoli
  • 12,145
  • 30
  • 95
  • 157
  • 1
    Read the step by step way to add the reference of [System.Management](http://stackoverflow.com/a/22223788/3240038) into your project. – Suhaib Janjua Apr 11 '17 at 23:09

5 Answers5

32

You need to add a reference to System.Management in your project.

MoominTroll
  • 2,502
  • 21
  • 24
13

I think the issue here is that you don't have a proper "pointer" to the System.Management assembly. You must:

  • Use a using statement - in some of your code, you didn't prefix classes w/ System.Management, so you should either include:

a.)

using System.Management

or

b.) a using block

using(System.Management) 
{  
//your code goes here
}

Next, you need to have a real assembly reference. To do this in Visual Studio, right click on your project (or on References) in the Solution Explorer window and click Add Reference. After the list tabulates, find System.Management.dll in the .NET tab. Once you add it, it should work!

Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
8

In the solution explorer:

  1. add reference
  2. find and add system.management in .NET class

that's it

Matthias
  • 7,432
  • 6
  • 55
  • 88
Ajay Tyagi
  • 81
  • 1
  • 1
3

Have you got the System.Management assembly referenced?

Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
2

Please first make sure you add the Library Systems.Management in to your project references.

Then just use that referred assembly by including in your class.

using System.Windows;
Liam
  • 27,717
  • 28
  • 128
  • 190
Khawaja Asim
  • 1,327
  • 18
  • 38