【发布时间】:2015-08-14 16:58:19
【问题描述】:
我有按类型获取网络接口名称的代码(例如 = 仅检测无线适配器)
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 托管网络虚拟适配器”
所以如果我指定了,则仅检测此适配器(如果可用)
请帮忙
【问题讨论】:
标签: c#