不囉嗦,直接上程式碼
private void UseHostNameToFindIP(string hostName)
{
IPAddress[] getIP;
try
{
//利用電腦名稱取得IP
getIP = Dns.GetHostAddresses(str_ClientHostName[i]);
//搜尋清單內的所有IP,因為會包含IPV6跟IPV4
for (int j = 0; j < getIP.Length; j++)
{
if (IPAddress.Parse(getIP[j].ToString()).AddressFamily
== AddressFamily.InterNetwork) //只找出IPV4的IP
{
Console.WriteLine(getIP[j].ToString());
}
}
}
catch (SocketException)
{
//找不到此電腦名稱就將IP設定成127.0.0.1
str_ContentClientIP[i] = "127.0.0.1";
}
}
沒有留言:
張貼留言