【问题标题】:get network interface by name not by type c#按名称获取网络接口而不是按类型c#
【发布时间】: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#


    【解决方案1】:

    这个怎么样

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

    【讨论】:

    • 给我们更多的代码,而不仅仅是一行。试试我编辑的答案
    • 问题是 cmbAdptors 显示的是类型而不是名称?
    • 在我上面的评论中我问你,也尝试在绑定之前添加 cmbAdaptors.DisplayMember = "Name"; cmbAdaptors.ValueMember = "名称";
    • 我已经得到了检测所有无线适配器的解决方案,我将重点放在特定的适配器属性上,因此它与我想要的相似,我将禁用该组合框。
    • 在 linq 查询之后为了显示名称执行此操作 foreach (var face in nics) cmbAdaptors.Items.Add(face.Name);刚刚测试!
    猜你喜欢
    • 2012-05-06
    • 2011-06-15
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多