几种C#程序读取MAC地址的方法求答案
展开全部
1通过IPConfig命令读取MAC地址///<summary///根据截取ipconfig /all命令的输出流获取网卡Mac///</summary///<returns</returnspublicstaticList<string
GetMacByIPConfig(){List<stringmacs=newList<string();ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig","/all");startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;Process p=Process.Start(startInfo);//截取输出流StreamReader reader=
p.StandardOutput;stringline=reader.ReadLine();while(!reader.EndOfStream){if(!string
.IsNullOrEmpty(line)){line=line.Trim();if(line.StartsWith("Physical Address")){macs.Add(line);}}line=reader.ReadLine();}//等待程序执行完退出进程
p.WaitForExit();
p.Close();reader.Close();returnmacs;}2 通过WMI读取MAC地址
1)该方法依赖WMI的系统服务,该服务一般不会被关闭;但如果系统服务缺失或者出现问题,该方法无法取得MAC地址。///<summary///通过WMI读取系统信息里的网卡MAC///</summary///<returns</returnspublicstaticList<string
GetMacByWMI(){List<stringmacs=newList<string();try{stringmac="";ManagementClass mc=newManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc){if((bool)mo["IPEnabled"]){mac=mo["MacAddress"
].ToString();
macs.Add(mac);}}moc=null;mc=null;}catch{}returnmacs;}3 通过NetworkInterface读取MAC地址
1)如果当前的网卡是禁用状态(硬件处于硬关闭状态),取不到该网卡的MAC地址,(您可以通过禁用网卡进行试验)。
2)如果当前启用了多个网卡,最先返回的地址是最近启用的网络连接的信息//返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
publicstaticNetworkInterface[] NetCardInfo(){returnNetworkInterface.GetAllNetworkInterfaces();}///<summary///通过NetworkInterface读取网卡Mac///</summary///<returns</returnspublicstaticList<string
GetMacByNetworkInterface(){List<stringmacs=newList<string();NetworkInterface[] interfaces=NetworkInterface.GetAllNetworkInterfaces();foreach(NetworkInterface niininterfaces){macs.Add(ni.GetPhysicalAddress().ToString());}returnmacs;}4 通过SendARP读取MAC地址///<summary///通过SendARP获取网卡Mac///网络被禁用或未接入网络(如没插网线)时此方法失灵///</summary///<param name="remoteIP"</param///<returns</returnspublicstaticstringGetMacBySendARP(stringremoteIP){StringBuilder macAddress=newStringBuilder();try{Int32 remote=inet_addr(remoteIP);
Int64 macInfo=newInt64();Int32 length=6;SendARP(remote,0,
refmacInfo,
reflength);stringtemp=Convert.ToString(macInfo,16
).PadLeft(12,'0'
).ToUpper();intx=12;for(inti=0; i<6; i++){if(i==5){macAddress.Append(temp.Substring(x-2,2));}else{macAddress.Append(temp.Substring(x-2,2)+"-");}x-=2;}returnmacAddress.ToString();}catch{returnmacAddress.ToString();}}[DllImport("Iphlpapi.dll")]privatestaticexternintSendARP(Int32 dest, Int32 host,
refInt64 mac,
refInt32 length);
[DllImport("Ws2_32.dll")]privatestaticexternInt32 inet_addr(stringip);5 从注册表读取MAC地址
常规用户可通过读取注册表项Windows Genuine Advantage获取到物理网卡地址。
GetMacByIPConfig(){List<stringmacs=newList<string();ProcessStartInfo startInfo = new ProcessStartInfo("ipconfig","/all");startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;Process p=Process.Start(startInfo);//截取输出流StreamReader reader=
p.StandardOutput;stringline=reader.ReadLine();while(!reader.EndOfStream){if(!string
.IsNullOrEmpty(line)){line=line.Trim();if(line.StartsWith("Physical Address")){macs.Add(line);}}line=reader.ReadLine();}//等待程序执行完退出进程
p.WaitForExit();
p.Close();reader.Close();returnmacs;}2 通过WMI读取MAC地址
1)该方法依赖WMI的系统服务,该服务一般不会被关闭;但如果系统服务缺失或者出现问题,该方法无法取得MAC地址。///<summary///通过WMI读取系统信息里的网卡MAC///</summary///<returns</returnspublicstaticList<string
GetMacByWMI(){List<stringmacs=newList<string();try{stringmac="";ManagementClass mc=newManagementClass("Win32_NetworkAdapterConfiguration");ManagementObjectCollection moc=mc.GetInstances();foreach(ManagementObject moinmoc){if((bool)mo["IPEnabled"]){mac=mo["MacAddress"
].ToString();
macs.Add(mac);}}moc=null;mc=null;}catch{}returnmacs;}3 通过NetworkInterface读取MAC地址
1)如果当前的网卡是禁用状态(硬件处于硬关闭状态),取不到该网卡的MAC地址,(您可以通过禁用网卡进行试验)。
2)如果当前启用了多个网卡,最先返回的地址是最近启用的网络连接的信息//返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
publicstaticNetworkInterface[] NetCardInfo(){returnNetworkInterface.GetAllNetworkInterfaces();}///<summary///通过NetworkInterface读取网卡Mac///</summary///<returns</returnspublicstaticList<string
GetMacByNetworkInterface(){List<stringmacs=newList<string();NetworkInterface[] interfaces=NetworkInterface.GetAllNetworkInterfaces();foreach(NetworkInterface niininterfaces){macs.Add(ni.GetPhysicalAddress().ToString());}returnmacs;}4 通过SendARP读取MAC地址///<summary///通过SendARP获取网卡Mac///网络被禁用或未接入网络(如没插网线)时此方法失灵///</summary///<param name="remoteIP"</param///<returns</returnspublicstaticstringGetMacBySendARP(stringremoteIP){StringBuilder macAddress=newStringBuilder();try{Int32 remote=inet_addr(remoteIP);
Int64 macInfo=newInt64();Int32 length=6;SendARP(remote,0,
refmacInfo,
reflength);stringtemp=Convert.ToString(macInfo,16
).PadLeft(12,'0'
).ToUpper();intx=12;for(inti=0; i<6; i++){if(i==5){macAddress.Append(temp.Substring(x-2,2));}else{macAddress.Append(temp.Substring(x-2,2)+"-");}x-=2;}returnmacAddress.ToString();}catch{returnmacAddress.ToString();}}[DllImport("Iphlpapi.dll")]privatestaticexternintSendARP(Int32 dest, Int32 host,
refInt64 mac,
refInt32 length);
[DllImport("Ws2_32.dll")]privatestaticexternInt32 inet_addr(stringip);5 从注册表读取MAC地址
常规用户可通过读取注册表项Windows Genuine Advantage获取到物理网卡地址。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询