http://expert.csdn.net/Expert/topic/2648/2648069.xml?temp=.7433435

解决方案 »

  1.   

    给一个笨一点的办法,借用
    ping -a ip
    -a就是解析ip为hostname,其实这里是调用了一个API来解析的,呵呵,具体是哪一个偶不知道了,你自己找找吧,或者就用这个笨方法吧,循环解析。
      

  2.   

    ping -a ip
    如果对应IP机器协议不全的话是无法解析主机名的
      

  3.   

    获得计算机名称和IP地址  --------------------------------------------------------------------------------
    源作者:追风                   人气:7120  
     
     
    VisualC#是微软公司推出的下一代程序开发语言,是微软.Net 框架中的的一个重要组成部分,在推出VisualC#的过程中,微软公司还推出了与之相对应的一个软件开发包--.Net FrameWorkSDK。此软件开发包里面封装了许多类、对象。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 stringgetIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得本机局域网IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName (Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
    return addr.ToString ( ) ;
    }
    源程序如下:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net;namespace GetIP
    {
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.ComponentModel.Container components = null;
      public Form1()
      {   
       InitializeComponent();
       label1.Text=System.Net.Dns.GetHostName();
       label2.Text=getIpAddress();
      }
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null) 
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }
      #region Windows Form Designer generated code
      private void InitializeComponent()
      {
       this.label1 = new System.Windows.Forms.Label();
       this.label2 = new System.Windows.Forms.Label();
       this.SuspendLayout();   this.label1.Location = new System.Drawing.Point(48, 24);
       this.label1.Name = "label1";
       this.label1.Size = new System.Drawing.Size(264, 24);
       this.label1.TabIndex = 0;
       this.label1.Text = "label1";   this.label2.Location = new System.Drawing.Point(48, 64);
       this.label2.Name = "label2";
       this.label2.Size = new System.Drawing.Size(264, 24);
       this.label2.TabIndex = 1;
       this.label2.Text = "label2";   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.ClientSize = new System.Drawing.Size(376, 221);
       this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label2,this.label1});
       this.Name = "Form1";
       this.Text = "Form1";
       this.ResumeLayout(false);
      }
      #endregion
      static void Main() 
      {
       Application.Run(new Form1());
      }
      private static string getIpAddress() 
      {
       System.Net.IPAddress addr;
       addr=new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address) ;
       return addr.ToString();
      }
     }
    }
     
     
      

  4.   

    参考代码
    获得局域网的主机名
    你必须先得到局域网内所有的IP地址(或IP地址范围),然后根据IP地址得到主机名。
    例如:
    string ipMask = "***.***.***.";
       for (int i = 128; i<= 255; i++)
       {
        string ipAddress = ipMask + i.ToString() ;
        IPHostEntry iphost = new IPHostEntry();
        try
        {
         iphost = Dns.Resolve(ipAddress);     IPAddress[] ipp1  = iphost.AddressList;
         foreach(IPAddress ip in ipp1)
         {
          listBox1.Items.Add("主机名称:"+iphost.HostName );
          listBox1.Items.Add("IP地址:"+ip.ToString());
         }    }
        catch
        {
        }
       }
    方法2(获得局域网的pc的Name)
    ArrayList arr = new ArrayList();
       this.listBox1.Items.Clear();
       DirectoryEntry root = new DirectoryEntry("WinNT:");
         {
       foreach(DirectoryEntry domain in root.Children)
       {
        foreach(DirectoryEntry computer in domain.Children)
        {
         arr.Add(computer.Name);
         this.listBox1.Items.Add(computer.Name+"->"+computer.Path);
        
     
        }
       }
       }
     获得主机名
    private string GetHostname()
      {
       string  m_Hostname = "";
       string m_IPstr = this.GetIPAddresses();   
       IPHostEntry iphost = new IPHostEntry();
       iphost = Dns.Resolve(m_IPstr); 
                m_Hostname = iphost.HostName;           
       return m_Hostname;
      }
    获得当前IP
      private string GetIPAddresses() 
      {
       string[] addresses = null;
       try 
       {
        ArrayList Temp = new ArrayList();
        ManagementObjectSearcher query = new ManagementObjectSearcher(
         "SELECT * FROM Win32_NetworkAdapterConfiguration ") ;
        ManagementObjectCollection queryCollection = query.Get();
        foreach( ManagementObject mo in queryCollection ) 
        {
         if ((bool)mo["IpEnabled"]) 
         {
          string[] ips = (string[])mo["IPAddress"];
          foreach (string s in ips) 
          {
           Temp.Add (s);
          }
         }
        }
        if (Temp.Count > 0) 
        {
         addresses = new string[Temp.Count];
         Temp.CopyTo (addresses);
        } 
        else 
        {
         addresses = new string[0];
        }
       } 
       catch (Exception) {} return addresses[0];
      }
      

  5.   

    foreach(DirectoryEntry domain in root.Children)
    {
    lb_pcName.Items.Add("★★★"+domain.Name+"★★★");
    foreach(DirectoryEntry pc in domain.Children)
    {
    if(pc.Name!="Schema")//Schema是结束标记
    lb_pcName.Items.Add(" "+pc.Name);
    }
    }