http://blog.bossma.cn/dotnet/csharp_winform_lan_get_ip_and_computername/
c#可以遍历局域网计算机,获取全部计算机的名称和IP地址,网上提供了相关的几种方法,并对效率进行了比较,但是没有对各种方法进行比较,以确定可以使用的情况。这篇文章将对这几种方法进行分析,以帮助了解各种方法适用的情况。
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Windows.Forms;
|
using System.Net.NetworkInformation;
|
using System.Collections;
|
using System.Diagnostics;
|
using System.DirectoryServices;
|
namespace SocketTransferFile
|
public partial class Form1 : Form
|
List<LocalMachine> machineList = new List<LocalMachine>();
|
lvLocalMachine.Items.Clear();
|
label4.Text = DateTime.Now.ToString();
|
foreach (LocalMachine machine in machineList)
|
ListViewItem item = new ListViewItem(new string[] { machine.Name, machine.IP });
|
lvLocalMachine.Items.Add(item);
|
label5.Text = DateTime.Now.ToString();
|
//获取Active Directory中的计算机节点
|
//label4.Text = DateTime.Now.ToString();
|
//label5.Text = DateTime.Now.ToString();
|
//label4.Text = DateTime.Now.ToString();
|
//label5.Text = DateTime.Now.ToString();
|
/// Handles the Click event of the button1 control.
|
/// <param name="sender">The source of the event.</param>
|
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
|
private void button1_Click(object sender, EventArgs e)
|
private void EnumComputersByPing()
|
for (int i = 1; i <= 254; i++)
|
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
|
string pingIP = "192.168.1." + i.ToString();
|
myPing.SendAsync(pingIP, 1000, null);
|
/// Handles the PingCompleted event of the _myPing control.
|
/// <param name="sender">The source of the event.</param>
|
/// <param name="e">The <see cref="System.Net.NetworkInformation.PingCompletedEventArgs"/> instance containing the event data.</param>
|
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;
|
ListViewItem item = new ListViewItem(new string[] { localMachine.Name, localMachine.IP });
|
lvLocalMachine.Items.Add(item);
|
/// 获取Active Directory中的计算机节点
|
private void EnumComputers()
|
using (DirectoryEntry root = new DirectoryEntry("WinNT:"))
|
foreach (DirectoryEntry domain in root.Children)
|
foreach (DirectoryEntry computer in domain.Children)
|
if (computer.Name == "Schema")
|
LocalMachine localMachine = new LocalMachine();
|
localMachine.IP = Dns.GetHostEntry(computer.Name).AddressList[0].ToString();
|
localMachine.Name = computer.Name;
|
ListViewItem item = new ListViewItem(new string[] { localMachine.Name, localMachine.IP });
|
lvLocalMachine.Items.Add(item);
|
private void GetAllLocalMachines()
|
Process p = new Process();
|
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.StandardInput.WriteLine("exit");
|
StreamReader reader = p.StandardOutput;
|
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
|
if (line.StartsWith(@"\\"))
|
string name = line.Substring(2).Trim();
|
//如果有路由器,会列出路由器,但是获取不到IP地址,会报错
|
LocalMachine localMachine = new LocalMachine();
|
localMachine.IP = Dns.GetHostEntry(name).AddressList[0].ToString();
|
localMachine.Name = name;
|
machineList.Add(localMachine);
|
这里使用了三种方法遍历局域网计算机。
1、获取当前域的计算机列表:GetAllLocalMachines
使用net命令,传递参数view,列出当前电脑所在域的所有计算机名称(完整的计算机名称)。
然后通过计算机名称获取IP地址。
适合局域网单一域的情况,测试了几次比DirectoryEntry的方式要快,不过没有进行大规模更广泛的测试。
可能无法获取工作组的服务器列表。
2、获取Active Directory中的计算机节点:EnumComputers
通过DirectoryEntry查找WinNT下的子节点,获取局域网中全部计算机的名称(完整的计算机名称)。
然后通过计算机名称获取IP地址。
可以获取局域网全部的计算机,不仅是某一个域的。速度比较慢。
3、获取指定IP范围内的计算机:EnumComputersByPing
适用c#自带的Ping,扫描当前计算机所在网段的IP地址,如果有回应,则证明计算机存在。
然后通过IP地址获取计算机名称(不一定是真正的计算机名称,可能是IIS的名称)。
需要指定IP段,不能获取真正的计算机名称。速度较快。如果需要过滤掉网关,还得自己写判断。
还有通过arp命令,和使用net命令类似,但是只能获取IP和mac地址,不能获取计算机名称。