c#可以遍历局域网计算机,获取全部计算机的名称和IP地址,网上提供了相关的几种方法,并对效率进行了比较,但是没有对各种方法进行比较,以确定可以使用的情况。这篇文章将对这几种方法进行分析,以帮助了解各种方法适用的情况。

 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();
} /// <summary>
/// 初始化数据
/// </summary>
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(); //获取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();
} /// <summary>
/// Handles the Click event of the button1 control.
/// </summary>
/// <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)
{
InitData();
} /// <summary>
/// 获取指定IP范围内的计算机
/// </summary>
private void EnumComputersByPing()
{
try
{
for (int i = ; i <= ; i++)
{
Ping myPing;
myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted); string pingIP = "192.168.1." + i.ToString();
myPing.SendAsync(pingIP, , null);
}
}
catch
{
}
} /// <summary>
/// Handles the PingCompleted event of the _myPing control.
/// </summary>
/// <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);
}
} /// <summary>
/// 获取Active Directory中的计算机节点
/// </summary>
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")
{
continue;
} try
{
LocalMachine localMachine = new LocalMachine();
localMachine.IP = Dns.GetHostEntry(computer.Name).AddressList[].ToString();
localMachine.Name = computer.Name; ListViewItem item = new ListViewItem(new string[] { localMachine.Name, localMachine.IP });
lvLocalMachine.Items.Add(item);
}
catch
{ }
}
}
}
} /// <summary>
/// 获取当前域的计算机列表
/// </summary>
/// <returns></returns>
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.Start();
p.StandardInput.WriteLine("exit"); StreamReader reader = p.StandardOutput; for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
line = line.Trim();
if (line.StartsWith(@"\\"))
{
string name = line.Substring().Trim(); //如果有路由器,会列出路由器,但是获取不到IP地址,会报错
try
{
LocalMachine localMachine = new LocalMachine(); localMachine.IP = Dns.GetHostEntry(name).AddressList[].ToString();
localMachine.Name = name; machineList.Add(localMachine);
}
catch
{
}
}
}
}
} public class LocalMachine
{
public string IP { get; set; }
public string Name { get; set; }
}
}

http://blog.bossma.cn/dotnet/csharp_winform_lan_get_ip_and_computername/

c# 遍历局域网计算机(电脑)获取IP和计算机名称的更多相关文章

  1. 通过js获取计算机内网ip,计算机名,mac地址

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  2. 如何让局域网其他电脑通过IP直接访问自己电脑的网站

    具体方法如下: 1. 打开系统的控制面板 2. 打开控制面板后打开window防火墙. 3.点击图中的“高级设置”选项. 4.点击图中的“本地计算机上的高级安全 Windows 防火墙”在右侧点击“W ...

  3. win7如何让局域网其他电脑通过IP直接访问自己电脑的网站

    一.打开控制面板 二.打开防火墙 三.点击右侧高级设置 四.点击防火墙属性 五.点击防火墙状态选择为关闭,确定 六.点击右侧允许或功能通过windows防火墙 七.执行第六部会打开防火墙通信例外窗口, ...

  4. (转)怎样查看局域网中自己的IP地址和其他电脑的IP地址?

    开始菜单->运行->打cmd,回车->再弹出的黑框里打ipconfig -all,回车显示的IP Address就是你的ip地址看局域网的电脑的ip用软件比较方便,比如p2p终结者, ...

  5. 使用Python获取计算机名,ip地址,mac地址等等

    获取计算机名 # 获取计算机名,常用的方法有三种 import os import socket # method one name = socket.gethostname() print(name ...

  6. 查看局域网其它电脑的计算机名和IP

    一.下面脚本可查看局域网中的电脑计算机名和IP,保存下面文本至记事本.后缀改成bat COLOR 0A CLS @ECHO Off Title 查询局域网内在线电脑IP :send @ECHO off ...

  7. 如何ping别人的计算机名来获取IP?

    来源:http://blog.csdn.net/qq_27109081/article/details/47128175 如何ping别人的计算机名来获取IP?            获取别人的IP, ...

  8. server2003中看不到网上邻居内容,其他电脑无法通过计算机名和IP访问本计算机(但网上邻居中可访问到)

    现象1:server2003中看不到网上邻居内容,查看工作组计算机看到的是空列表, 现象2:其他电脑无法通过计算机名和IP访问本计算机(但网上邻居中可访问到)   访问提示:--Windows 200 ...

  9. vue配置手机通过IP访问,Win10让局域网内其他电脑通过IP访问网站的方法

    vue配置手机通过IP访问config/index.js// Various Dev Server settings host: '0.0.0.0', // can be overwritten by ...

随机推荐

  1. AD19新功能之Gloss Selected(修线)

    一.强大的修线功能 鼠标从右下往左上框选线,然后按 tab 键,选中需要修的走线 然后执行 “Route”栏下 “Gloss Selected”命令进行修线: 二.循环至推模式 AD19默认为推挤模式 ...

  2. ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    pip install imagededup 时,报错:Cannot uninstall 'wrapt'. It is a distutils installed project and thus w ...

  3. jmeter+jenkins 配置过程(很详细)

    一.安装jmeter 第一步  安装JDK,配置JDK路径.注:jdk下载地址,推荐使用jdk1.8版本 http://www.oracle.com/technetwork/java/javase/d ...

  4. RedisTemplate的key默认序列化器问题

    原文:https://blog.csdn.net/skymouse2002/article/details/80736577 redis的客户端换成了spring-boot-starter-data- ...

  5. 提速1000倍,预测延迟少于1ms,百度飞桨发布基于ERNIE的语义理解开发套件

    提速1000倍,预测延迟少于1ms,百度飞桨发布基于ERNIE的语义理解开发套件 11月5日,在『WAVE Summit+』2019 深度学习开发者秋季峰会上,百度对外发布基于 ERNIE 的语义理解 ...

  6. 瞎扯KMP

    瞎扯\(KMP\) 众所周知,\(KMP\)是一种玄学的字符串模式匹配算法. 什么是字符串模式匹配? 通俗的讲,就是统计一个字符串(通常很长)中某个子串(即一段连续的字符)出现的次数或位置.一般来说, ...

  7. java中使用redis --- List列表的简单应用

    1.Dos中启动server端 2.idea中启动client端 public class RedisTest01 { public static void main(String[] args){ ...

  8. AtCoder Beginner Contest 143 F - Distinct Numbers

    题意 给出一个长度为NNN的序列,求对于所有k∈[1,N]k\in[1,N]k∈[1,N],每次从序列中选出kkk个互不相同的数,最多能取多少次. N≤3e5N\le3e5N≤3e5 题解 我们首先把 ...

  9. nginx配置url伪静态

    rewrite 规则 定向路径 重写类型; 举例: rewrite  (.*)/web/(.*)-(.*)-(.*).html$  $1/web/index.php?r=$2/$3/$4  last; ...

  10. 012——C#打开ecxel修改数据(附教程)

    (一)参考文献:[C#]将数据写入已存在的excel文件 C# 导入excel数据,解决关闭excel后不能释放资源的问题 (二)视频教程:https://v.qq.com/x/page/p30063 ...