How To: Obtain the MAC address

G

Guest

Hello,

Thanks for reviewing my question. I would like to obtain the MAC address of
my network card in C#. Is there any simple API I can call.

Many Thanks
Peter
 
G

Guest

Have a look at the system.management namespace and WMI from there you can get
the information that you are looking for.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

ManagementObjectSearcher wmiObj= new ManagementObjectSearcher("select
MACAddress from Win32_NetworkAdapter");

foreach (ManagementObject item in wmiObj.Get())
string s = item.Item["MACAddress"];



Should do it

Cheers,
 
G

Guest

Ignacio,

Thanks for the tip. Question though, it lists out several MAC address but
only one is my network card's MAC address. Is there any way of filter the
query?

Also, ManagementObjectSearcher and about querying is all new to me. Is
there a book you could recommend that discusses this class?

Thanks again
Peter

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

ManagementObjectSearcher wmiObj= new ManagementObjectSearcher("select
MACAddress from Win32_NetworkAdapter");

foreach (ManagementObject item in wmiObj.Get())
string s = item.Item["MACAddress"];



Should do it

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Peter said:
Hello,

Thanks for reviewing my question. I would like to obtain the MAC address of
my network card in C#. Is there any simple API I can call.

Many Thanks
Peter
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Are you getting more than one ?
Do you have more than one nic?

You can check the AdapterType , like this:

ManagementObjectSearcher("select AdapterType, MACAddress from
Win32_NetworkAdapter");


then you can check if the adapter is ethernet, take a look at
Win32_NetworkAdapter class in the MSDN

I dont know of any particular book about WMI, it should exist though.

By the way, my code contained couple of errors
string s = item.Item["MACAddress"];

will give you an error, please check that, the same with item.Item , when it
should say item[""]

I had that example around but it's written in VB :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Peter said:
Ignacio,

Thanks for the tip. Question though, it lists out several MAC address but
only one is my network card's MAC address. Is there any way of filter the
query?

Also, ManagementObjectSearcher and about querying is all new to me. Is
there a book you could recommend that discusses this class?

Thanks again
Peter

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

ManagementObjectSearcher wmiObj= new ManagementObjectSearcher("select
MACAddress from Win32_NetworkAdapter");

foreach (ManagementObject item in wmiObj.Get())
string s = item.Item["MACAddress"];



Should do it

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Peter said:
Hello,

Thanks for reviewing my question. I would like to obtain the MAC
address
of
my network card in C#. Is there any simple API I can call.

Many Thanks
Peter
 
G

Guest

Ignacio,

That's better - it worked. Question - is the query syntax in any way like
SQL? For instance, could I have queried "select AdapterType="Ethernet
802.3", MACAddress from Win32_NetworkAdapter" or this select AdapterType=0,
MACAddress from Win32_NetworkAdapter"

This querying seems rather strange. I am use to just passing in structure
and calling an API. Is this querying only for WMI?

Again Thanks.
Peter

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

Are you getting more than one ?
Do you have more than one nic?

You can check the AdapterType , like this:

ManagementObjectSearcher("select AdapterType, MACAddress from
Win32_NetworkAdapter");


then you can check if the adapter is ethernet, take a look at
Win32_NetworkAdapter class in the MSDN

I dont know of any particular book about WMI, it should exist though.

By the way, my code contained couple of errors
string s = item.Item["MACAddress"];

will give you an error, please check that, the same with item.Item , when it
should say item[""]

I had that example around but it's written in VB :)

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Peter said:
Ignacio,

Thanks for the tip. Question though, it lists out several MAC address but
only one is my network card's MAC address. Is there any way of filter the
query?

Also, ManagementObjectSearcher and about querying is all new to me. Is
there a book you could recommend that discusses this class?

Thanks again
Peter

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

ManagementObjectSearcher wmiObj= new ManagementObjectSearcher("select
MACAddress from Win32_NetworkAdapter");

foreach (ManagementObject item in wmiObj.Get())
string s = item.Item["MACAddress"];



Should do it

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Hello,

Thanks for reviewing my question. I would like to obtain the MAC address
of
my network card in C#. Is there any simple API I can call.

Many Thanks
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top