本人想遍历整个局域网,获取IP和主机名。搜索网络发现有一个Ping方法。源代码如下。
------------------------
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.Net.NetworkInformation; 
using System.IO; 
using System.Collections; 
using System.Diagnostics; 
using System.DirectoryServices; 
using System.Management; 
  
namespace SocketTransferFile 

    /// <summary> 
    /// 
    /// </summary> 
    public partial class Form1 : Form 
    { 
        //局域网计算机列表 
        List<LocalMachine> machineList = new List<LocalMachine>(); 
  
        //Form构造函数 
        public Form1() 
        { 
            InitializeComponent(); 
            InitData(); 
        } 
 private void InitData() 
        { 
            lvLocalMachine.Items.Clear(); 
            machineList.Clear(); 
  
            //获取当前域的计算机列表 
            label4.Text = DateTime.Now.ToString(); 
            GetAllLocalMachines(); 
  
            foreach (LocalMachine machine in machineList) 
            { 
                ListViewItem item = new ListViewItem(new string[] { machine.Name, machine.IP }); 
                lvLocalMachine.Items.Add(item); 
            } 
            label5.Text = DateTime.Now.ToString(); 
  
            
        } 
 private void button1_Click(object sender, EventArgs e) 
        { 
            InitData(); 
        } 
private void EnumComputersByPing() 
        { 
            try
            { 
                for (int i = 1; i <= 254; i++) 
                { 
                    Ping myPing; 
                    myPing = new Ping(); 
                    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted); 
  
                    string pingIP = "192.168.1." + i.ToString(); 
                    myPing.SendAsync(pingIP, 1000, null); 
                } 
            } 
            catch
            { 
            } 
        } 
private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e) 
        { 
            if (e.Reply.Status == IPStatus.Success) 
            { 
                LocalMachine localMachine = new LocalMachine(); 
                localMachine.IP = e.Reply.Address.ToString(); 
                //localMachine.Name =Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName; 
                //localMachine.Name = Dns.Resolve(e.Reply.Address.ToString()).HostName; 
              localMachine.Name = Dns.GetHostEntry(e.Reply.Address.ToString()).HostName; 
                ListViewItem item = new ListViewItem(new string[] { localMachine.Name, localMachine.IP }); 
                lvLocalMachine.Items.Add(item); 
            } 
        } 
public class LocalMachine 

    public string IP { get; set; } 
    public string Name { get; set; } 

}
  ------------------------在VS2008编译,提示GetHostByAddress已过时,更改成Dns.GetHostEntry()。然后发现遍历出的IP很快,几乎1s时间。但是发现listview中的主机名除了自己以外,全部都是IP。什么原因,请大家帮忙,本人是新手,请大家说的细一点。