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. FastDateFormat日期工具类

    原文:https://blog.csdn.net/u013823538/article/details/80197476 public class DateUtil { public static f ...

  2. 安装完conda后,进入终端显示(base)

    通过配置auto_activate_base关闭自动进入conda基础环境: conda config --set auto_activate_base false 想要重新开启就设为true就行了 ...

  3. 遍历SQL SERVER中所有存储过程和触发器

    如果需要查找某个存储过程或触发器中是否含有某段文本(比如:你想知道有哪些存储过程操作了某个表) 可以这么写 select name from sysobjects o, syscomments s w ...

  4. Vue实现一个图片懒加载插件(转载)

    Vue是可以自定义指令的,最近学习过程中遇见了一个需要图片懒加载的功能,最后参考了别人的代码和思路自己重新写了一遍.以下将详细介绍如何实现自定义指令v-lazyload. 先看如何使用这个指令: &l ...

  5. HTTP/TCP/IP UDP Socket等区别联系

    1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. ...

  6. vue 下拉框单选、多选以及默认值

    背景: 单选框和多选框 都是使用了 el-select,但传给后端的值类型不一样,多选框传的值是 list类型: ['value1','value2'] ,单选框传值和其他类型一样:设置默认值也是如此 ...

  7. plsql工具使用

    作者:lhrbest 作者:张冲andy 作者:jack_孟

  8. jquery手机端产品列表响应式宽高检测宽度赋值给高度让宽高相同

    在手机端浏览商品列表,开发人员懂得设计图片做正好的尺寸,那样浏览很好,但有人天生泛懒图片都是随手上传不管大小合适不合适,要求还是响应式的.今天就用jquery检测宽度,并赋值给高度来实现错位问题 &l ...

  9. 对linux下日志文件error监控

    对日志文件中的error进行监控,当日志文件中出现error关键字时,就截取日志(grep -i error 不区分大小写进行搜索"error"关键字,但是会将包含error大小写 ...

  10. PostgreSQL 慢查询SQL语句跟踪

    示例:启用 SQL 跟踪PostgreSQL 日志支持的输出格式有 stderr(默认), csvlog , syslog 一般的错误跟踪,只需在配置文件 [postgresql.conf]简单设置几 ...