簡體   English   中英

通過名稱而不是通過類型c#獲取網絡接口

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

我有按類型獲取網絡接口名稱的代碼(例如,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;
        }
    }

這是代碼

但我希望它按名稱獲取網絡無線適配器

對於ex =如果適配器的名稱為“ Microsoft托管網絡虛擬適配器”

因此,如果我指定了該選項,則僅檢測此適配器(如果有)

請幫助

這個怎么樣

 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