可能重複:
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