2012년 01월 27일
MAC address
public 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;
}
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
//for each j you can get the MAC
PhysicalAddress address = nics[0].GetPhysicalAddress();
byte[] bytes = address.GetAddressBytes();
string ip = string.Empty;
for (int i = 0; i < bytes.Length; i++)
{
//// Display the physical address in hexadecimal.
////Console.Write("{0}", bytes[i].ToString("X2"));
ip += bytes[i].ToString("X2");
// Insert a hyphen after each byte, unless we are at the end of the
// address.
if (i != bytes.Length - 1)
{
////Console.Write("-");
ip += "-";
}
}
this.txtMacAddress.Text = ip;
# by | 2012/01/27 16:32 | [ C# ] | 트랙백 | 덧글(0)























