我在获取MAC地址是出现"未处理COMException"异常是怎么回事,下面是我的代码:string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
异常出现在循环读取的地方,好像是queryCollection.count引发的异常,该如何解决呀?
这个是因为你的本机中的mac地址有问题,或者你的系统本身有问题了,不是你的程序的问题,建议重装系统
获取mac地址代码如下:
string mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementOjbectCollection moc = mc.GetInstances();
foreach(ManagementObject moin moc)
{
if(mo["IPEnabled"].ToString() == "True")
{
mac = mo["MacAddress"].ToString();
}
}
ManagementClass mc;
ManagementObjectCollection moc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
moc = mc.GetInstances();
string str = "";
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
str = mo["MacAddress"].ToString();
break;
}
}
return str;