C#实现 获取IPv4

1、对于Web应用程序开发:

            Request.ServerVariables.Get("Local_Addr").ToString();

2、其他

  引入命名空间

   using System.Net;
   using System.Net.Sockets;

 

   主要代码:

                //从IP地址列表中筛选出IPv4类型的IP地址   
                //AddressFamily.InterNetwork表示此IP为IPv4,
                //AddressFamily.InterNetworkV6表示此地址为IPv6类型  

            IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());


            for (int i = 0; i < IpEntry.AddressList.Length; i++)
            {   
                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    return IpEntry.AddressList[i].ToString();
                }
            }

 

你可能感兴趣的:(C#,c#,程序开发,web)