当前位置:网站首页>C# 32. Select a network card from the static class implementation drop-down box

C# 32. Select a network card from the static class implementation drop-down box

2022-06-09 18:35:00 lljss2020

//NetIf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;

namespace Xxxxx
{
    
    class NetIf
    {
    
        public struct NetIfType
        {
    
            public string netIf;
            public string Ip;
        }
        public static List<NetIfType> NetIfList = new List<NetIfType>();
        public static int selectNetIf = 0;


        public static List<NetIfType> GetNetIf()
        {
    
            List<NetIfType> listStr = new List<NetIfType>();
            // Get all network card information 
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface adapter in nics)
            {
    
                // Determine whether it is an Ethernet card 
                //Wireless80211  wireless network adapter  Ppp  broadband connection 
                //Ethernet  Ethernet card  
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
    
                    // Get Ethernet card network interface information 
                    IPInterfaceProperties ip = adapter.GetIPProperties();
                    // Get unicast address set 
                    UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
                    foreach (UnicastIPAddressInformation ipadd in ipCollection)
                    {
    
                        //InterNetwork IPV4 Address  InterNetworkV6 IPV6 Address 
                        //Max MAX  Address 
                        if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
    
                            listStr.Add(new NetIfType()
                            {
    
                                netIf = adapter.Description,
                                Ip = ipadd.Address.ToString()
                            });
                        }
                    }
                }
            }
            return listStr;
        }

        // Select local on network card IP
        public static string GetLocalIp(int index)
        {
    
            if (index>= 0)
            {
    
                return NetIfList[index].Ip;
            }
            else
            {
    
                return null;
            }
        }
    }
}

private void comboBoxNetIf_DropDown(object sender, EventArgs e)
{
    
    NetIf.NetIfList.Clear();
    NetIf.NetIfList = NetIf.GetNetIf();
    comboBoxNetIf.Items.Clear();
    foreach (NetIf.NetIfType m in NetIf.NetIfList)
    {
    
        comboBoxNetIf.Items.Add(m.netIf);
    }           
}


// In Africa UI In the thread 
private void UserThread()
{
    
 		string localIp = null;
            int flag;
            
            Invoke((EventHandler)(delegate
            {
    
                NetIf.selectNetIf = comboBoxNetIf.SelectedIndex;
            }));
            localIp = NetIf.GetLocalIp(NetIf.selectNetIf);

.......
}

The actual effect :
 Insert picture description here

原网站

版权声明
本文为[lljss2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091816082357.html