Friday, January 30, 2009

Getting MAC address using .Net

What is MAC address

Media Access Control address (MAC address) is a unique address assigned to all network adapters or network cards.It is normally expressed as group of 6 two digit hex numbers.ie MAC address is of length 48bits or 6 bytes.

Eg:00-0B-CD-95-CA-8F

You can see your network card's MAC address by typing the command

ipconfig -all

More details here

Retrieving MAC address using C#.Net

We can retrieve MAC address using .Net only through Management namespace.You can get a basic idea about classes in Management namespace by referring below links.

http://msdn.microsoft.com/en-us/library/system.management.aspx

System.Management.ManagementObjectSearcher Class [Articles]

Before going to work with System.Management namespace add a reference to the assembly System.Management which contains the said namespace.

There are 2 methods to find out the MAC address using Management classes.

1.Using the class ManagementClass

This is the simplest method which creates an object of ManagementClass by passing the string parameter "Win32_NetworkAdapterConfiguration" through it's constructor.Then get the ManagementObjectCollection using the method GetInstances().Once we get the collection we can iterate through it and find the MAC address which is IP enabled.

private string GetMACUsingManagementClass()
{
ManagementClass mgmtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection CollectionOfResults = mgmtClass.GetInstances();
foreach (ManagementObject obj in CollectionOfResults)
{
if ((bool)obj.Properties["IPEnabled"].Value)
{
string mac = obj.Properties["MACAddress"].Value.ToString();
return mac;
}
}
return string.Empty;
}


2.Using Query



This method employs an query to find out correct ManagementObject.The query is similar as any sql query.We need to create object of ManagementObjectSearcher class by passing the query through it's constructor.Then call it's Get method which gives ManagementObjectCollection.This collection can be iterated to get the MAC address.



private string GetMACUsingQuery()
{
ManagementObjectSearcher Searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE");
ManagementObjectCollection CollectionOfResults = theSearcher.Get();
foreach (ManagementObject CurrentObject in CollectionOfResults)
{
string macAdd = CurrentObject.Properties["MACAddress"].Value.ToString();
return macAdd;
}
return string.Empty;
}


VB.Net Code to find out MAC address



Method 1 : Management class



Private Function GetMACUsingManagementClass() As String
Dim
mgmtClass As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim CollectionOfResults As ManagementObjectCollection = mgmtClass.GetInstances()
For Each objec In CollectionOfResults
If CType(objec.Properties("IPEnabled").Value, Boolean) Then
Dim
mac As String = objec.Properties("MACAddress").Value.ToString()
Return mac
End If
Next
objec
Return String.Empty
End Function


Method 2: Using query



Private Function GetMACUsingQuery() As String

Dim
Searcher As New ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
Dim CollectionOfResults As ManagementObjectCollection = Searcher.Get()
For Each CurrentObject In CollectionOfResults
Dim macAdd As String = CurrentObject.Properties("MACAddress").Value.ToString()
Return macAdd
Next CurrentObject
Return String.Empty
End Function



A WIME(Works In My Environment ie P4 32Bit+Vista+VS2008) Sample can be downloaded from here.



NB: This retrieves MAC address of local system under local privileges.If you need to retrieve MAC address of a remote system you can try following links.



Get MAC address of client machine using C#



Developersdex.com - Get the MAC Address of your Network Card (C# Code)



win32 programmer wmi How can I obtain the MAC address of a remote

No comments: