c#中获取本机所有的IPv6地址

    技术2022-05-11  69

    需要使用命名空间:using System.Net.NetworkInformation;

    下述代码中,textBox2是一文本框,用于显示每个接口的描述字串,ipListComb是一组合列表框,存放所获得的IP地址.

    private void DisplayAllAddresses()  {    NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();    int i=0;    foreach (NetworkInterface adapter in adapters)      {

            IPInterfaceProperties adapterProperties = adapter.GetIPProperties();        UnicastIPAddressInformationCollection allAddress =                                             adapterProperties.UnicastAddresses;       if (allAddress.Count  > 0)         {          textBox2.Text +="interface "+i+"description:/n/t"+adapter.Description+"/n";                    i++;           foreach (UnicastIPAddressInformation addr in allAddress)             {               if (addr.Address.AddressFamily ==AddressFamily.InterNetworkV6)                       ipListComb.Items.Add(addr.Address);              }          }     } } 


    最新回复(0)