能获取到像“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的标识)
这个具体要怎么改?
下面是我获取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的标识)
这个具体要怎么改?
(若批评不自由则赞美无意义...) 那个设置是无效的,不知道怎么回事,我把它修改之后,获取的值就不一样。
ipEntry.AddressList 获取的列表值中有10进制数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;
}