/// <summary>
/// 得到MAC地址
/// </summary>
/// <returns></returns>
public string GetMacAddress()
{
ManagementClass mc;
ManagementObjectCollection moc;
try
{
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
}
catch
{
return "";
}
//****先得到网卡数目
int i = 0;
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
i++;
}
mo.Dispose();
}
//***赋值给数组
ManagementClass mc_2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc_2 = mc_2.GetInstances();
string[] array = new string[i];
int j = 0;
foreach (ManagementObject mo in moc_2)
{
if ((bool)mo["IPEnabled"] == true)
{
string temp = mo["MacAddress"].ToString();
temp = temp.Replace(":", "-");
array[j] = temp;
j++;
}
mo.Dispose();
}
string retv = "";
foreach (string s in array)
retv += s;
if (retv.Length > 17)
{
retv = retv.Substring(0, 17);
}
return retv;
}