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. LeetCode:137. 只出现一次的数字 II

    LeetCode:137. 只出现一次的数字 II 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. ...

  2. jade注释

    在jade里面注释是有多种方式的 单行注释,加两个//,跟js里面一样 //h1.title#title imoock jade study => <!--h1.title#title i ...

  3. 用Xcode配置完美ACMer环境

    用Xcode配置完美ACMer环境 前言 ​ 作为\(ACMer\),需求大致为强大的文本编辑功能\((VIM)\),便捷的文件模版功能以及多文件编译功能.\(vscode\)虽然强大,但是与集成\( ...

  4. 重新编程Hexiwear Docking Station扩展坞

    Hexiwear扩展坞(Docking Station)有一个很不错的功能:它板载了一个调试电路(OpenSDA). 这样我就不需要一个外部调试器来调试Hexiwear. 但是,重新编程扩展坞自身需要 ...

  5. Ignatius and the Princess IV (简单DP,排序)

    方法一:    直接进行排序,输出第(n+1)/2位置上的数即可. (容易超时,关闭同步后勉强卡过) #include<iostream> #include<cstdio> # ...

  6. nginx url默认去掉index.php

  7. 遇见zTree和chrome的俩坑

    今天后台系统发现一bug,就是前几天用zTree做的树形结构,今下午突然不好使了,然后就查问题.我自己电脑装的是chrome浏览器,后台debug一看传的json数据,没毛病,想当然的断定不是数据的问 ...

  8. postgresql学习笔记--基础篇 - copy

    1. psql 导入/导出数据 psql支持文件数据导入到数据库,也支持数据库表数据导出到文件中. COPY命令和\copy 命令都支持这两类操作,但两者有如下区别: COPY 命令是SQL命令,\c ...

  9. TCP单线程实现并发

    服务端 from gevent import monkey;monkey.patch_all() import socket from gevent import spawn server = soc ...

  10. BZOJ4706 B君的多边形 (超级卡特兰数/施罗德数)

    题目 权限题QAQ 题解 超级卡特兰数/施罗德数 CODE #include <bits/stdc++.h> using namespace std; const int MAXN = 1 ...