Wednesday, October 8, 2008

โค้ด C# ดึงข้อมูล MAC Address

Get MAC Address with C#.

Ref : codeproject.com

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
using System.Collections;

class .....
{
public static string GetMACAddress()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}

MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
}

No comments:

Post a Comment

Popular Posts