我根据那个代码简单编写了一个软件
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Collections.Generic;
class App
{
static void Main()
{
// 脚本执行
DoGetHostAddresses(Dns.GetHostName());
Console.WriteLine("GetPhysicalIP:");
GetPhysicalIP();
}
public static void DoGetHostAddresses(string hostname)
{
IPAddress[] addresses = Dns.GetHostAddresses(hostname);
Console.WriteLine("GetHostAddresses({0}) returns:", hostname);
foreach (IPAddress address in addresses)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(" {0}", address);
}
}
}
// 获取物理网卡IP
public static List<string> GetPhysicalIP()
{
List<string> ipList = new List<string>();
List<string> macList = Mac(onlyEnabled: true);
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet)
{
continue;
}
PhysicalAddress address = adapter.GetPhysicalAddress();
if (!macList.Contains(address.ToString()))
{
continue;
}
IPInterfaceProperties ipProperties = adapter.GetIPProperties();
UnicastIPAddressInformationCollection ipCollection = ipProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation ip in ipCollection)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(ip.Address.ToString());
ipList.Add(ip.Address.ToString());
}
}
}
return ipList;
}
public static List<string> Mac(bool onlyEnabled = false)
{
List<string> macList = new List<string>();
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
if (onlyEnabled && adapter.OperationalStatus != OperationalStatus.Up)
{
continue;
}
PhysicalAddress address = adapter.GetPhysicalAddress();
macList.Add(address.ToString());
}
return macList;
}
}
【 在 VincentGe 的大作中提到: 】
: 那个程序上是没有问题的,根据知乎反编译提供的代码,可以认为没有问题。
: [upload=1][/upload]
: #发自zSMTH@CDU.MP
--
FROM 118.81.10.*