可能重复:
Get IPv4 addresses from Dns.GetHostEntry()获得通过C#IP地址(IPv4中值)
我尝试下面的代码,让我的IP地址,在XP中,它返回的IP地址V4格式, ,但是当我在我的系统(Windows 7)中执行它时,它会以v6格式返回IP地址。
如何解决这个问题?
代码
try
{
//iphostname = Dns.GetHostName(); // Resolving Host name
IPHostEntry ipentry = Dns.GetHostEntry(hostLabel.Text);
IPAddress[] addr = ipentry.AddressList;// Resolving IP Addresses
for (int i = 0; i < addr.Length; i++)
{
try
{
ipLabel.Text = Convert.ToString(addr[i]) + "\r\n";
}
catch
{
ipLabel.Text += "IP Address | " + "\r\n";
}
}
}
catch
{
//richTextBox1.Text += "Hostname | " + "\r\n";
}
尝试寻找这里:http://stackoverflow.com/questions/1059526/get-ipv4-addresses-from-dns-gethostentry – 2012-08-09 09:27:21
“IP地址” 不存在 - 一台机器可以有多个IP地址。 v6和v4地址不是同一件事的两种表示--v6地址与v4地址是分开的。 – 2012-08-09 09:30:00