zoukankan      html  css  js  c++  java
  • c#(WinForm)遍历局域网计算机(电脑)获取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 = 1; i <= 254; i++)
                    {
                        Ping myPing;
                        myPing = new Ping();
                        myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);
      
                        string pingIP = "192.168.1." + i.ToString();
                        myPing.SendAsync(pingIP, 1000, 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[0].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(2).Trim();
      
                        //如果有路由器,会列出路由器,但是获取不到IP地址,会报错
                        try
                        {
                            LocalMachine localMachine = new LocalMachine();
      
                            localMachine.IP = Dns.GetHostEntry(name).AddressList[0].ToString();
                            localMachine.Name = name;
      
                            machineList.Add(localMachine);
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
      
    public class LocalMachine
    {
        public string IP { get; set; }
        public string Name { get; set; }
    }
    }
  • 相关阅读:
    (转)修改Android解锁界面
    linux terminal 快捷键
    (转)Android蓝牙开发浅析
    (转)android 编译单个模块
    (转)Android关机AppWidget的实现
    MyEclipse修改页面模板(JSP和HTML等) 分类: WEB项目应用 20100131 00:03 698人阅读 评论(1) 收藏
    定时任务:Timer类、TimerTask类 分类: java 20100317 22:01 551人阅读 评论(0) 收藏
    Socket读取输入流 分类: java 20100322 17:38 3330人阅读 评论(0) 收藏
    [Microsoft][SQLServer 2000 Driver for JDBC]Object has been closed. 分类: 开发常见问题解决方案 20100318 22:05 1106人阅读 评论(0) 收藏
    3/17/10 9:55:59 AM CST: [INFO] User settings file does not exist C:/Documents and Settings/Administrator/.m2/settings.xml 分类: 开发常见问题解决方案 20100317 10:20 3448人阅读 评论(4) 收藏
  • 原文地址:https://www.cnblogs.com/hyruur/p/2252760.html
Copyright © 2011-2022 走看看