您的位置:首页 > 编程语言 > C#

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

2007-03-01 09:50 274 查看
需要使用命名空间: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);
              }
          }
     }
 }
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# textbox interface