c#(WinForm)遍历局域网打定机(电脑)获得IP战打定机称号

c#可以遍历局域网打定机,获得齐部打定机的称号战IP地址,网上供给了相干的几种法子,并对功用进行了对比,但是出有对各类法子进行对比,以确定可以操纵的景遇。这篇文章将对这几种法子进行解析,以资助相识各类法子适用的景遇。

usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Net; usingSystem.Net.NetworkInformation; usingSystem.IO; usingSystem.Collections; usingSystem.Diagnostics; usingSystem.DirectoryServices; usingSystem.Management;   namespaceSocketTransferFile {     ///     publicpartialclassForm1 : Form     {         //局域网打定机列表         List machineList = newList();           //Form机关函数         publicForm1()         {             InitializeComponent();             InitData();         }           ///         privatevoidInitData()         {             lvLocalMachine.Items.Clear();             machineList.Clear();               //获稳当前域的打定机列表             label4.Text = DateTime.Now.ToString();             GetAllLocalMachines();               foreach(LocalMachine machine inmachineList)             {                 ListViewItem item = newListViewItem(newstring[] { machine.Name, machine.IP });                 lvLocalMachine.Items.Add(item);             }             label5.Text = DateTime.Now.ToString();               //获得Active Directory中的打定机节点             //label4.Text = DateTime.Now.ToString();             //EnumComputers();             //label5.Text = DateTime.Now.ToString();               //获得指定IP局限内的打定机             //label4.Text = DateTime.Now.ToString();             //EnumComputersByPing();             //label5.Text = DateTime.Now.ToString();         }           ///         ///         ///         privatevoidbutton1_Click(objectsender, EventArgs e)         {             InitData();         }           ///         privatevoidEnumComputersByPing()         {             try            {                 for(inti = 1; i ......                {                     Ping myPing;                     myPing = newPing();                     myPing.PingCompleted += newPingCompletedEventHandler(_myPing_PingCompleted);                       stringpingIP = "192.168.1."+ i.ToString();                     myPing.SendAsync(pingIP, 1000, null);                 }             }             catch            {             }         }           ///         ///         ///         privatevoid_myPing_PingCompleted(objectsender, PingCompletedEventArgs e)         {             if(e.Reply.Status == IPStatus.Success)             {                 LocalMachine localMachine = newLocalMachine();                 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;                   ListViewItem item = newListViewItem(newstring[] { localMachine.Name, localMachine.IP });                 lvLocalMachine.Items.Add(item);             }         }           ///         privatevoidEnumComputers()         {             using(DirectoryEntry root = newDirectoryEntry("WinNT:"))             {                 foreach(DirectoryEntry domain inroot.Children)                 {                     foreach(DirectoryEntry computer indomain.Children)                     {                         if(computer.Name == "Schema")                         {                             continue;                         }                           try                        {                             LocalMachine localMachine = newLocalMachine();                             localMachine.IP = Dns.GetHostEntry(computer.Name).AddressList[0].ToString();                             localMachine.Name = computer.Name;                               ListViewItem item = newListViewItem(newstring[] { localMachine.Name, localMachine.IP });                             lvLocalMachine.Items.Add(item);                         }                         catch                        {                           }                     }                 }             }         }           ///         ///         privatevoidGetAllLocalMachines()         {             Process p = newProcess();             p.StartInfo.FileName = "net";             p.StartInfo.Arguments = "view";             p.StartInfo.UseShellExecute = false;             p.StartInfo.RedirectStandardInput = true;             p.StartInfo.RedirectStandardOutput = true;             p.StartInfo.RedirectStandardError = true;             p.StartInfo.CreateNoWindow = true;             p.Start();             p.StandardInput.WriteLine("exit");               StreamReader reader = p.StandardOutput;               for(stringline = reader.ReadLine(); line != null; line = reader.ReadLine())             {                 line = line.Trim();                 if(line.StartsWith(@"\\"))                 {                     stringname = line.Substring(2).Trim();                       //假如有路由器,会列前程由器,但是获得没有到IP地址,会报错                     try                    {                         LocalMachine localMachine = newLocalMachine();                           localMachine.IP = Dns.GetHostEntry(name).AddressList[0].ToString();                         localMachine.Name = name;                           machineList.Add(localMachine);                     }                     catch                    {                     }                 }             }         }     } } 

..........
09-05 09:29