[C#] 纯文本查看 复制代码 //得到本机内网IP
string s = "";//定义一个变量,用来记录IP地址
System.Net.IPAddress[] addressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;//获取IP地址列表
for (int i = 0; i < addressList.Length; i++)//遍历IP地址列表
{
s += addressList[i].ToString() + "\n";//获得遍历到的IP地址
}
//s就是本机内网ip
[C#] 纯文本查看 复制代码 /// <summary>
/// 获得本机公网IP
/// </summary>
/// <returns>获取不到则返回空串</returns>
private static string GetIP()
{
string tempip = "";
try
{
WebRequest wr = WebRequest.Create("http://www.ip138.com/ips138.asp");
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int start = all.IndexOf("您的IP地址是:[") + 9;
int end = all.IndexOf("]", start);
tempip = all.Substring(start, end - start);
sr.Close();
s.Close();
}
catch
{
}
return tempip;
}
|