C#获取IP的方式有很多种,这里通过http://www.ipip.net/这个稳定的在线IP库的来获取公网IP。
string tempip = "0.0.0.0";
WebRequest wr = WebRequest.Create("http://www.ipip.net/");
Stream s = wr.GetResponse().GetResponseStream();
if (s != null)
{
StreamReader sr = new StreamReader(s, Encoding.UTF8);
string all = sr.ReadToEnd();
int start = all.IndexOf("您当前的IP:", StringComparison.Ordinal) + ;
int end = all.IndexOf("<", start, StringComparison.Ordinal);
tempip = all.Substring(start, end - start);
sr.Close();
s.Close();
}