获取mac地址 ,之前电脑是win7 32的 后来 装了64位的了 为什么会出现异常(:System.ComponentModel.Win32Exception: 系统找不到指定的文件。(红色异常处))?求解public string getmacs()
{
string hip = "";
IPHostEntry oiphost = Dns.Resolve(Environment.MachineName);
if (oiphost.AddressList.Length > 0)
hip = oiphost.AddressList[0].ToString();
string dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-a " + hip;
psi.UseShellExecute = false;
proc = Process.Start(psi); //:System.ComponentModel.Win32Exception: 系统找不到指定的文件。
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); Match m = Regex.Match(dirResults, "\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w\\w");
if (m.ToString() != null)
{
return m.ToString();
}
else
return "";
}
{
string hip = "";
IPHostEntry oiphost = Dns.Resolve(Environment.MachineName);
if (oiphost.AddressList.Length > 0)
hip = oiphost.AddressList[0].ToString();
string dirResults = "";
ProcessStartInfo psi = new ProcessStartInfo();
Process proc = new Process();
psi.FileName = "nbtstat";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "-a " + hip;
psi.UseShellExecute = false;
proc = Process.Start(psi); //:System.ComponentModel.Win32Exception: 系统找不到指定的文件。
dirResults = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); Match m = Regex.Match(dirResults, "\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w\\w");
if (m.ToString() != null)
{
return m.ToString();
}
else
return "";
}
{
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.Replace(":", "-"));
}
这样 就可以了
http://www.cnblogs.com/deckard/archive/2009/04/07/1430807.html
http://blog.163.com/yangwenwei2008@126/blog/static/6179273520111016453339/
参考
using System.Linq;
using System.Net.NetworkInformation;//忽略回环和隧道(虚拟网卡)
var omitted = NetworkInterfaceType.Loopback | NetworkInterfaceType.Tunnel;
var mac = NetworkInterface.GetAllNetworkInterfaces().First(ni => (omitted & ni.NetworkInterfaceType) != ni.NetworkInterfaceType).GetPhysicalAddress();
//mac.ToString()