我有按类型获取网络接口名称的代码(例如,ex =仅检测无线适配器)
private void Form1_Load(object sender, EventArgs e)
{
/// Detecting Wireless Adaptors Using Linq
IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && network.Name == "Microsoft Hosted Network Virtual Adapter");
////Modified by to select only the active wireless adaptor by using below Linq statement
////.Where(network => network.OperationalStatus == OperationalStatus.Up && network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
////To detect all Ethernet and wireless adaptors you can use below statement
////.Where(network => network.OperationalStatus == OperationalStatus.Up && (network.NetworkInterfaceType == NetworkInterfaceType.Ethernet || network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211))
///Add Items To Drop Down List
cmbAdptors.DisplayMember = "Description";
cmbAdptors.ValueMember= "Id";
foreach (NetworkInterface item in nics)
{
cmbAdptors.Items.Add(item);
}
if (cmbAdptors.Items.Count > 0)
cmbAdptors.SelectedIndex = 0;
}
private void cmbAdptors_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (cmbAdptors.SelectedItem is NetworkInterface)
{
slectedNic = cmbAdptors.SelectedItem as NetworkInterface;
uniCastIPInfo = null;
///Populating IPv4 address
if (slectedNic != null && slectedNic.GetIPProperties().UnicastAddresses != null)
{
UnicastIPAddressInformationCollection ipInfo = slectedNic.GetIPProperties().UnicastAddresses;
foreach (UnicastIPAddressInformation item in ipInfo)
{
if (item.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
lblIP.Text = item.Address.ToString();
uniCastIPInfo = item;
break;
}
}
}
BandwidthCalculator(uniCastIPInfo, slectedNic);
wirelssUpdator.Enabled = true;
}
}
catch (Exception ex)
{
throw;
}
}
这是代码
但我希望它按名称获得网络无线适配器
for ex =如果适配器名称为“Microsoft Hosted Network Virtual Adapter”
所以,如果我指定,只检测此适配器(如果可用)
请帮忙
答案 0 :(得分:0)
这个怎么样
IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(network => network.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 && network.Name == "name you want");
foreach(var nIf in nics)
cmbAdaptors.Items.Add(nIf.Name);