本帖最后由 argent10 于 2010-02-03 16:11:17 编辑

解决方案 »

  1.   

    我的最后结果是显示出VMware的地址了,但我想显示出本机的外网IP
      

  2.   

    只贴获取IP部分的代码:private void button1_Click(object sender, EventArgs e)
    {
    textBox1.Text = Dns.Resolve(Dns.GetHostName()).AddressList.GetValue(0).ToString(); //内网IP
    textBox2.Text = GetIP(); //外网IP
    }static string GetIP()
    {
    Uri uri = new Uri("http://www.ciker.net/ip/index.asp");
    System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    req.ContentLength = 0;
    req.CookieContainer = new System.Net.CookieContainer();
    req.GetRequestStream().Write(new byte[0], 0, 0);
    System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
    StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
    string s = rs.ReadToEnd();
    rs.Close();
    req.Abort();
    res.Close();
    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"您的IP:(?<IP>[0-9\.]*)");
    if (m.Success)
    {
    return m.Groups["IP"].Value;
    }
    else
    {
    return "Failed..";
    }
      

  3.   

    System.Net.IPHostEntry ips = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); foreach (System.Net.IPAddress ip in ips.AddressList) { Console.WriteLine(ip.ToString()); } 
    要获取本机出口的外网IP,实际是需要利用访问某个可以返回本机出口的外网IP的internet资源,例如: 本站的ip提供程序访问此IP提供程序,将只返回一个你的出口IP的字符串,使用起来是超简单的,代码如下:
    using (System.Net.WebClient wc = new System.Net.WebClient()) { Console.WriteLine(wc.DownloadString("http://www.zu14.cn/ip/")); } Console.ReadLine(); 
      

  4.   

    可以访问外网主机,取得WAN的IP,比如,你可用用 webRequest 读取 www.ip138.com 然后在分析返回结果.
      

  5.   


    要想获取外网IP,必须让数据包往外面走一圈再回来
    楼上说的读取www.ip138.com,分析IP,是可行的