What's Here?
- Members: 300,497
- Replies: 826,157
- Topics: 137,487
- Snippets: 4,420
- Tutorials: 1,148
- Total Online: 1,850
- Members: 79
- Guests: 1,771
|
This is a snippet that can be used to retrieve the MAC address of a network adapter
|
Submitted By: PsychoCoder
|
|
Rating:
 
|
|
Views: 11,241 |
Language: C#
|
|
Last Modified: February 10, 2008 |
|
Instructions: Add a reference to the System.Management Namespace, call the method and the value will be returned in string format |
Snippet
//Namespace reference
using System.Management;
/// <summary>
/// Returns MAC Address from first Network Card in Computer
/// </summary>
/// <returns>MAC Address in string format</returns>
public string FindMACAddress()
{
//create out management class object using the
//Win32_NetworkAdapterConfiguration class to get the attributes
//af the network adapter
ManagementClass mgmt = new ManagementClass ("Win32_NetworkAdapterConfiguration");
//create our ManagementObjectCollection to get the attributes with
ManagementObjectCollection objCol = mgmt.GetInstances();
string address = String.Empty;
//loop through all the objects we find
foreach (ManagementObject obj in objCol)
{
if (address == String.Empty) // only return MAC Address from first card
{
//grab the value from the first network adapter we find
//you can change the string to an array and get all
//network adapters found as well
if ((bool)obj["IPEnabled"] == true) address = obj["MacAddress"].ToString();
}
//dispose of our object
obj.Dispose();
}
//replace the ":" with an empty space, this could also
//be removed if you wish
address = address.Replace(":", "");
//return the mac address
return address;
}
Copy & Paste
|
|
|
|