简体   繁体   English

通过名称而不是通过类型c#获取网络接口

[英]get network interface by name not by type c#

i have code that get network interface name by type (for ex = detecting wireless adapters only) 我有按类型获取网络接口名称的代码(例如,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;
        }
    }

here is the code 这是代码

but i want that it get network wireless adapter by name 但我希望它按名称获取网络无线适配器

for ex = if adapter has name "Microsoft Hosted Network Virtual Adapter" 对于ex =如果适配器的名称为“ Microsoft托管网络虚拟适配器”

so if i specify that , detect only this adapter if available 因此,如果我指定了该选项,则仅检测此适配器(如果有)

Pls help 请帮助

How about this 这个怎么样

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM