能获取到像“192.168.1.1”的IP值吗?或者还是需要转换之类的方法。
下面是我获取IP的方法    /// <summary>
    /// 获取本机IP
    /// </summary>
    /// <returns></returns>
    private string GetIP()
    {
        string hostName = Dns.GetHostName(); //得到主机名
        IPHostEntry ipEntry = Dns.GetHostEntry(hostName);//得到主机IP
        string ip = ipEntry.AddressList[0].ToString();
        return ip;
    }我把host文件改成
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host# localhost name resolution is handled within DNS itself.
# 127.0.0.1       localhost
# 192.168.1.110   localhost
127.0.0.1 www.template.com
127.0.0.1 www.youxi.com
红色是我修改的部分, 改完之后获取的IP是::1(都是localhost的标识)
这个具体要怎么改?

解决方案 »

  1.   

    foreach(... in ipEntry.AddressList)ps:跟win不win7没关系...
      

  2.   

    同样的设置我在XP,2003中都是10进制数的IP。
      

  3.   

    vrhero 
    (若批评不自由则赞美无意义...) 那个设置是无效的,不知道怎么回事,我把它修改之后,获取的值就不一样。
      

  4.   

    host文件仅仅是本地dns,你还要设置网卡才行...
      

  5.   

    知道了。
     ipEntry.AddressList 获取的列表值中有10进制数IP,
    判断一下,获取到了。
    谢谢了。
      

  6.   

    现在不用这个方法了,它一直获取的是服务器IP,对于各个用户访问的网站的客户端IP是获取不到的。
    具体判断:public static string GetIP()
            {
                string hostName = Dns.GetHostName(); //得到主机名
                IPHostEntry ipEntry = Dns.GetHostEntry(hostName);//得到主机IP
                string ip = "";
                foreach (IPAddress i in ipEntry.AddressList)
                {
                    if (i.ToString().Split('.')[0] != "")
                    {
                        ip = i.ToString();
                    }
                    else
                    {
                        ip = "127.0.0.1";
                    }  
              }
                return ip;
            }
      

  7.   

    楼上的还是16的原因是,8楼的代码有个小错误,既判断IP地址地方有误还有就是他的判断有误因为他获得IP后没有跳出循环,这样判断就无意义了