C# 获取当前主机网络(公网)IP地址
1、百度搜索IP地址第一项会显示当前主机IP,点进去发现是
www.ip138.com 获取的
直接提交:“http://1111.ip138.com/ic.asp”就会返回IP地址

2、首先窗体添加一个文本框,一个按钮,双击按钮进入事件
无视其它,谢谢。

3、 private void button1_Click(object sender, EventArgs e)
{
try
{
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
byte[] pageData = wc.DownloadData("http://1111.ip138.com/ic.asp");
string SourceCode = Encoding.UTF8.GetString(pageData);
// saveSourceCode(SourceCode);
wc.Dispose();
}
catch (Exception x)
{
throw new Exception("获取异常," + x.Message);
}
Match rebool = Regex.Match(SourceCode, @"\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
string value = rebool.Value;
textBox1.Text = value;//文本框1获得的就是网络IP地址
}