按名称获取网络接口不被键入c#
问题描述:
我有按类型获取网络接口名称代码(EX =检测无线适配器只)按名称获取网络接口不被键入c#
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;
}
}
这里是代码
,但我想,它的名字获得网络无线适配器
为EX =如果适配器有名为“微软托管的网络虚拟适配器”
所以,如果我指定,只检测该适配器,如果可
请帮助
答
这个怎么样
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);
它不工作。帮帮我 ! – aditya
给了我们更多的代码,而不仅仅是一条线。尝试我编辑的答案 –
上面给出的几乎完整的代码 – aditya