winform程序 如何获取客户机的本地ip地址
发布网友
发布时间:2022-11-02 10:26
我来回答
共1个回答
热心网友
时间:2023-10-23 08:28
#region[内网地址]
public static string getClientLocalIPAddress()
{
string localIP = null;
try
{
IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
IPAddress ipaddress = ipHost.AddressList[0];
localIP = ipaddress.ToString();
return localIP;
}
catch
{
return "unknown";
}
finally
{
}
}
#endregion
#region[*地址]
public static string getClientInternetIPAddress()
{
string internetAddress = "";
try
{
using (WebClient webClient = new WebClient())
{
internetAddress = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
//判断IP是否合法
if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
{
internetAddress = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
}
}
return internetAddress;
}
catch
{
return "unknown";
}
finally
{
}
}
#endregion