try {
    IPHostEntry hostInfo = Dns.Resolve(hostString);
    // Get the IP address list that resolves to the host names contained in the 
    // Alias property.
    IPAddress[] address = hostInfo.AddressList;
    // Get the alias names of the addresses in the IP address list.
    String[] alias = hostInfo.Aliases;    Console.WriteLine("Host name : " + hostInfo.HostName);
    Console.WriteLine("\nAliases : ");
    for(int index=0; index < alias.Length; index++) {
      Console.WriteLine(alias[index]);
    } 
    Console.WriteLine("\nIP Address list :");
    for(int index=0; index < address.Length; index++) {
       Console.WriteLine(address[index]);
    }
 }
 catch(SocketException e) 
 {
    Console.WriteLine("SocketException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(ArgumentNullException e)
 {
Console.WriteLine("ArgumentNullException caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
 }
 catch(NullReferenceException e)
 {
     Console.WriteLine("NullReferenceException caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }
 catch(Exception e)
 {
     Console.WriteLine("Exception caught!!!");
     Console.WriteLine("Source : " + e.Source);
     Console.WriteLine("Message : " + e.Message);
 }

解决方案 »

  1.   

    楼上的那个获得的好像不是自己的公网IP我的这个是找的,不知道可不可以
    Get External IP Address, CryptoAPI File Encryption, Proxy/FireWall code This is an app I wrote to update a Dynamic IP DNS Service when an IP address changes, Gets External IP address the easy way, Has support for Proxy Server's stores information in registry encrypted with CryptoAPI If you have questions you can email me at [email protected] however due to high email volume It might take a day or two to get back to ya. Dan 
    http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=e30534db-5e28-4868-9a2c-96662728fda1可能可以满足你的需求
      

  2.   

    IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
    IPAddress ipAddress = ipHostInfo.AddressList[0];
    Console.WriteLine(ipAddress);
      

  3.   

    TO:
    xiaoslong(龙哥) ,按照你的方法只能得到内部的IP地址啊。(192.168.0.2)TO:
    shuker(我是一只小花猪) ,我的Hostname如果填机器名得不了外部IP啊?(202.104.237.179)TO:
    cnming(cnming) ,我的这个外部IP是通过拔号得到的,可能是城域网的IP。但不知道电信通过什么转换,因为这个IP做成WEB服务器全国都可以访问...还有
      

  4.   

    TO:
    cnming(cnming) ,我的这个外部IP是通过拔号得到的,可能是城域网的IP。但不知道电信通过什么转换,因为这个IP做成WEB服务器全国都可以访问...还有你给我的那个网址打不开:~
      

  5.   

    你在dos控制台里面输入
    ipconfig /all能得到自己的外部ip吗?
      

  6.   

    ipconfig /all
    里得不到自己的外部IP,只能得到内网IP,DNS,网关等信息。
      

  7.   

    to cnming(cnming) ( ) 
    这个我也看了,得访问WEB服务器
    不知看年NDSI或RAW方面的知识可否实现这个,当然就不是。NET的类库了
      

  8.   

    找别人在外网做个webService把你的外部ip返回给你的程序。
      

  9.   

    用Visual C#获得计算机名称和IP地址
    Visual C#是微软公司推出的下一代程序开发语言,是微软.Net 框架中的的一个重要组成部分,在推出Visual C#的过程中,微软公司还推出了与之相对应的一个软件开发包--.Net FrameWork SDK。此软件开发包里面封装了许多类、对象。Visual C#就是通过调用这些类、对象来实现许多比较强大的功能。
    在.Net FrameWork SDK中提供了二个可用于网络编程的名称空间,一个是System.Net,另一个是System..Net.Socket。本文就是利用第一个名称空间中封装的类和对象来读取本地计算机名称和机器中所有的IP地址。一.概述:
    我们知道对于一台计算机来说,他只有一个计算机名称,但是他可以有多个IP地址。例如当计算机通过拨号上网的时候,在验证完用户名和口令以后,就会动态分配一个IP地址,此时计算机就拥有了二个IP地址,一个时自己设定的局域网用的IP地址,另外一个就是拨号上网动态分配的IP地址了。本文就是来探索一下如何读取此二个IP地址和计算机名称。二.程序设计和运行的环境:
    (1)微软公司视窗2000服务器版
    (2).Net FrameWrok SDK Beta 2版三.程序设计的主要思路及实现方法:
    (1).读取计算机的名称:
    在名称空间System.Net中定义了一个类Dns,在此类中定义了一个比较重要的方法 GetHostName ( ),此方法的返回值就是本地计算机名称。在程序设计中首先要导入System.Net名称空间,然后通过调用Dns类中的GetHostName ( )方法读取本地计算机名称,具体实现的主要语句如下:
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
    (2).读取计算机的拨号上网临时的IP地址和局域网分配的固定IP地址:
    在程序设计中,我们是通过一个自定义的函数--getIPAddress ( )来读取IP地址的。首先看一下如何读取本地固定的IP地址的。在Dns类中还定义了一个方法GetHostByName ( )。此方法的返回值时IPHostEntry 对象,此对象有一个属性是AddressList,此属性是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息。这当中也就包含了拨号上网得到的临时分配的IP地址和局域网固定的IP地址。具体实现语句如下:
    private static string getIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得本机局域网IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
    return addr.ToString ( ) ;
    } 四.读取计算机名称本机固定IP地址源程序 
    IP01.cs源程序:
    //导入程序用到的名称空间
    using System ;
    using System.Net;
    using System.Windows.Forms ;
    using System.Drawing ;
    public class Form3 : Form
    {
    //定义二个标签
    private Label label1 ;
    private Label label2 ;
    public static void Main ( )
    {
    Application.Run ( new Form3 ( ) ) ;
    }
    // 构造窗体
    public Form3 ( )
    {
    // 建立标签并且初始化
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    //先继承一个Label 类
    label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
    label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
    //设定 Label的显示位置
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
    // 显示本机的计算机名称
    label2.Text = "IP 地址:" + getIPAddress ( ) ;
    // 显示本机的局域网IP地址
    label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
    label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
    //设定标签的大小
    label1.TabIndex = 0 ;
    label2.TabIndex = 1 ;
    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    // 设定标签的对齐方式
    this.Text = "获得主机名称和IP地址!" ;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
    // 设定窗体的边界类型
    this.ForeColor = System.Drawing.SystemColors.Desktop ;
    this.Font = new System.Drawing.Font ( "宋体" , 10 , System.Drawing.FontStyle.Bold ) ;
    // 设定字体、大小就字体的式样
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
    this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
    //把标签加到窗体中
    this.Controls.Add ( this.label1 ) ;
    this.Controls.Add ( this.label2 ) ;
    }
    private static string getIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得本机局域网IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
    return addr.ToString ( ) ;

    }
     在经过以下编译命令编译后,
    csc /r:system.dll /r:system.windows.forms.dll /r:system.drawing.dll /t:winexeip01.cs
    得到ip01.exe文件,此文件就可以读取本地固定的IP地址。以下是执行后的界面: 五.读取计算机名称和拨号上网动态分配的IP地址源程序
    在前面已经说了,GetHostByName ( )方法的返回值时IPHostEntry 对象,此对象的属性 AddressList,是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息。在ip01.cs中AddressList [0].Address是固定的IP地址,而对于上网动态分配的IP地址是.AddressList [1].Address。据此我们可以的得到读取拨号上网动态分配的IP地址的源程序:
    IP02.cs源程序:
    //导入程序用到的名称空间
    using System ;
    using System.Net;
    using System.Windows.Forms ;
    using System.Drawing ;
    public class Form3 : Form
    {
    //定义二个标签
    private Label label1 ;
    private Label label2 ;
    public static void Main ( )
    {
    Application.Run ( new Form3 ( ) ) ;
    }
    // 构造窗体
    public Form3 ( )
    {
    // 建立标签并且初始化
    this.label1 = new System.Windows.Forms.Label ( ) ;
    this.label2 = new System.Windows.Forms.Label ( ) ;
    //先继承一个Label 类
    label1.Location = new System.Drawing.Point ( 24 , 16 ) ;
    label2.Location = new System.Drawing.Point ( 44 , 36 ) ;
    //设定 Label的显示位置
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
    // 显示本机的计算机名称
    label2.Text = "IP 地址:" + getIPAddress ( ) ;
    // 显示本机的拨号动态分配IP地址
    label1.Size = new System.Drawing.Size ( 200 , 50 ) ;
    label2.Size = new System.Drawing.Size ( 200 , 80 ) ;
    //设定标签的大小
    label1.TabIndex = 0 ;
    label2.TabIndex = 1 ;
    label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ;
    // 设定标签的对齐方式
    this.Text = "获得主机名称和IP地址!" ;
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent ;
    this.AutoScaleBaseSize = new System.Drawing.Size ( 8 , 16 ) ;
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D ;
    // 设定窗体的边界类型
    this.ForeColor = System.Drawing.SystemColors.Desktop ;
    this.Font = new System.Drawing.Font ( "宋体" , 10 , System.Drawing.FontStyle.Bold ) ;
    // 设定字体、大小就字体的式样
    this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide ;
    this.ClientSize = new System.Drawing.Size ( 250 , 250 ) ;
    //把标签加到窗体中
    this.Controls.Add ( this.label1 ) ;
    this.Controls.Add ( this.label2 ) ;
    }
    private static string getIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得拨号动态分配IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [1].Address ) ;
    return addr.ToString ( ) ;
    }
    } 编译完成后,执行可得到如下运行界面: 图02:读取计算机名称和动态IP地址六.总结:
    本文是通过二个例子来读出机器的计算机名称和不同的IP地址,通过以上二个例子,我们可以看到如果机器有三个或者更多的IP地址,我们也可以通过设定AddressList的不同值来得到机器不同的IP地址。
    在名称空间System.Net中还提供了很多面向网络编程的的类,这些类的功能是十分巨大的,灵活运用这些类,可以开发出很多功能强大的网络应用程序。
      

  10.   

    楼上的方法对我这种用adal medom集体上网的人就不行啊
    用AddressList只能得到一个值,就是本地ip。
      

  11.   

    >TO:
    >cnming(cnming) ,我的这个外部IP是通过拔号得到的,可能是城域网的IP。但不知道电信通过什么转换,因为这个IP做成WEB服务器全国都可以访问...还有你给我的那个网址打不开:~
    gotdotnet的网站刷新几次即可打开
    〉〉外部IP是通过拔号得到的,可能是城域网的IP。
    不大明白,如果你每次拨号,全国都可以访问,那么说明这个IP就是全国的局域网IP,当然就是Internet IP了,怎么会是局域网IP另外,Internet访问局域网内的服务器也不稀奇,通过端口映射即可我给你的那个网址也只是看英文说明里头说可以满足你的需求,但是实际的我不是很清楚,因为我没有试过
      

  12.   

    OK,这一帖我采取webService的方法,谢谢大家,敬请期待下一期问题:)~