c# 多網絡卡 由【網路介面卡名】獲取網絡卡資訊,IP
阿新 • • 發佈:2018-11-15
c# 多網絡卡 由【網路介面卡名】獲取網絡卡資訊,IP
多網絡卡電腦中,網路介面卡的名字 多樣化!
- 專案中需要,根據網路介面卡 名字 獲取 單個網絡卡的IP:
using System.Net.NetworkInformation;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetIPv4AddressByAdapterName("乙太網2"));
Console.ReadLine();
}
private static string GetIPv4AddressByAdapterName(string adapterName)
{
NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in adapters)
{
if (adapter.Name == adapterName)
{
UnicastIPAddressInformationCollection unicastIPAddressInformation = adapter.GetIPProperties().UnicastAddresses;
foreach (var item in unicastIPAddressInformation)
{
if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
return item.Address.ToString();
}
}
}
}
return string.Empty;
}
}