C# 获取网卡IP地址
NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
{
IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
{
if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(UnicastIPAddressInformation.Address);
}
}
}
foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
{
IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
{
if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
{
Console.WriteLine(UnicastIPAddressInformation.Address);
}
}
}