正确代码using System;
using System.Net;
namespace DNSTest
{ public class DNSUtility
{
public static int Main(string[] args)
{
String strHostName;
if (args.Length==0)
{
//获得本地的IP地址
//首先获得本地机的主机名
strHostName=Dns.GetHostName();
Console.WriteLine("本地机的主机名为:"+strHostName);
}
else
{
strHostName=args[0];
} //接着使用主机名获取IP地址列表
//一台主机可能不只一个IP
IPHostEntry ipEntry=Dns.GetHostByName(strHostName);
IPAddress[] addr=ipEntry.AddressList;
for (int i=0;i<addr.Length;i++)
{
Console.WriteLine("IP Address {0}:{1}",i,addr[i].ToString());
}
return 0;
}
}
}运行通过。

解决方案 »

  1.   

    以前有人问过,记得这两个地方是错的:
    String strHostName=new String("");//改为string strHostName;
    bate1为DNS,bate2为Dns
      

  2.   

    对于字符串类型String 并不需要通过构造函数来对其赋初值.
      

  3.   

    String strHostName=new String("")->String strHostName or String strHostName=@""
    DNS->Dns
      

  4.   

    如果获得机器的IP地址或根据IP得到机器名称 :以下代码
    using System;
    using System.Net;
    class proxy_set{
    public void proxyHost(string s_host){
    WebProxy proxyObject = new WebProxy("http://10.127.0.1:80/",true);
    Uri siteUri = new Uri(s_host);
    if (proxyObject.IsBypassed(siteUri)){
        Console.WriteLine("This is LocalIPAddress , do not use proxy!");
        //WebRequest req = WebRequest.Create(siteUri);
        //req.Proxy=proxyObject;
        
    }
    else{
    Console.WriteLine("This is not LocalIPAddress , do use proxy!");
    }//if
    }
    }//class end
    //=========================================================================
    class ResolveDNS{
    IPAddress[] m_arrayIPs;
    public void Resolve(string s_host){
    IPHostEntry ip=Dns.GetHostByName(s_host);
    m_arrayIPs=ip.AddressList;
    }
    //indexer
    public IPAddress this[int nindex]{
    get{
    return m_arrayIPs[nindex];
    }
    }
    //indexer end
    //-------------------------------
    //progerty
    public int IPLength{
    get{
    return m_arrayIPs.Length;
    }
    }
    //progerty end
    }
    //class end
    //==============================================================================
    class testAPP{
    public static void Main(string[] args){
    proxy_set proxy_1=new proxy_set();
    ResolveDNS resolve1=new ResolveDNS();

    //======================================================
    try{
    proxy_1.proxyHost("http://"+args[0]);
    resolve1.Resolve(args[0]);
    int n=resolve1.IPLength;
    Console.WriteLine("Get IP address .");
    Console.WriteLine();
    for(int i=0;i<n;i++){
    Console.WriteLine(resolve1[i]);
    }
    }//try
    catch(Exception e){
    Console.WriteLine("Finding a Wrong :{0}",e.Message);
    Console.WriteLine("Stoping ......!");
    }

    //=======================================================

    }
    }
      

  5.   

    得到ip地址和得到机器名称代码集成在一起:// created on 2001-11-5 at 9:18
    /*
     * Get HostName by IP , or IP by HostName
     * Input parameter one:    IP
     *                         Host
     * Output parameter  two:  HostName
     *                         IP Address
     */
     using System;
     using System.Net;
     
     class IP_set{
      string s_hostname;
      public void host_name(string ip){
      IPAddress myip=IPAddress.Parse(ip);
      IPHostEntry myhost=Dns.GetHostByAddress(myip);
      s_hostname=myhost.HostName;
      }
      public string get_name{
      get{
      return s_hostname;
      }
      }
     }//class end
     //=======================================================
     class Host_set{
    IPAddress[] m_arrayIPs;
    public void Resolve(string s_host){
    IPHostEntry ip=Dns.GetHostByName(s_host);
    m_arrayIPs=ip.AddressList;
    }
    //indexer
    public IPAddress this[int nindex]{
    get{
    return m_arrayIPs[nindex];
    }
    }
    //indexer end
    //-------------------------------
    //progerty
    public int IPLength{
    get{
    return m_arrayIPs.Length;
    }
    }
    //progerty end
    }
    //class end
    //============================================================class Test{
    public static void Main(string[] args){
    string condition;
    condition=args[0];
    try{
    switch (condition){
    case "IP":
    case "ip":

            IP_set my_Host=new IP_set();
            try{
               my_Host.host_name(args[1]);
           Console.WriteLine("Alias of the IP Address ----->{0}",my_Host.get_name);
            }
            catch(Exception e){
           Console.WriteLine("Finding Wrong is :{0}",e.Message);
           Console.WriteLine("Now , Stoping......");
            }
    break;
    case "host":
    case "Host":
    Host_set resolve1=new Host_set();
            //proxy_set proxy_1=new proxy_set();
            //======================================================
            try{
            //proxy_1.proxyHost("http://"+args[0]);
            resolve1.Resolve(args[1]);
            int n=resolve1.IPLength;
            Console.WriteLine("Get IP address .");
            Console.Write("your IP Address----->");
            for(int i=0;i<n;i++){
            Console.WriteLine(resolve1[i]);
            }
            }//try
            catch(Exception e){
            Console.WriteLine("Finding Wrong is :{0}",e.Message);
            Console.WriteLine("Now ,Stoping ......!");
            }
    break;
    }//switch
        }//try
        catch(Exception e1){Console.WriteLine("Error one is :{0}",e1.Message);
        }//catch
        
    }
    }