public static Dictionary<string, string> GetDefaultAddressInfos()
{
Dictionary<string, string> pairs = new Dictionary<string, string>();
var ip = from nic in NetworkInterface.GetAllNetworkInterfaces()
let searchSub = from p in nic.GetIPProperties().UnicastAddresses
where p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address)
select p
where
nic.OperationalStatus == OperationalStatus.Up
&& (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
&& searchSub.Any() // && nic.GetIPProperties().GatewayAddresses.Count>0
select new { PhysicalAddress = nic.GetPhysicalAddress().ToString(), IPAddress = (searchSub).FirstOrDefault()?.Address.ToString() };
ip.ToList().ForEach(ipx => pairs.Add(ipx.PhysicalAddress, ipx.IPAddress));
return pairs;
}
public static Dictionary<string, string> GetDefaultAddressInfos()
{
Dictionary<string, string> pairs = new Dictionary<string, string>();
var ip = from nic in NetworkInterface.GetAllNetworkInterfaces()
let searchSub = from p in nic.GetIPProperties().UnicastAddresses
where p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address)
select p
where
nic.OperationalStatus == OperationalStatus.Up
&& (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
&& searchSub.Any() // && nic.GetIPProperties().GatewayAddresses.Count>0
select new { PhysicalAddress = nic.GetPhysicalAddress().ToString(), IPAddress = (searchSub).FirstOrDefault()?.Address.ToString() };
ip.ToList().ForEach(ipx => pairs.Add(ipx.PhysicalAddress, ipx.IPAddress));
return pairs;
}
private static string GetMacAddressByNetworkInformation()
{
string key = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\";
string macAddress = string.Empty;
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
&& adapter.GetPhysicalAddress().ToString().Length != 0)
{
string fRegistryKey = key + adapter.Id + "\Connection";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
if (rk != null)
{
string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
if (fPnpInstanceID.Length > 3 &&
(fPnpInstanceID.Substring(0, 3) == "PCI" || fPnpInstanceID.Contains("PCI")))
{
macAddress = adapter.GetPhysicalAddress().ToString();
break;
}
}
}
}
}
catch (Exception)
{
//这里写异常的处理
}
return macAddress;
}