• 大小: 42KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-11
  • 语言: C#
  • 标签: C#  IP地址  MAC地址  

资源简介

C#获取局域网内所有联网设备的IP地址和MAC地址

资源截图

代码片段和文件信息

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.Threading;
using System.Net;
using System.Collections;
using System.Diagnostics;
using System.IO;

namespace findDevice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public class LocalMachine
        {
            public string MachineIP { get; set; }
            public string MachineMAC { get; set; }
            public string MachineName { get; set; }
        }

        static ArrayList GetAllLocalMachines()
        {
            Process p = new Process();
            p.StartInfo.FileName = “cmd.exe“;
            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(“arp -a“);
            p.StandardInput.WriteLine(“exit“);
            ArrayList list = new ArrayList();
            StreamReader reader = p.StandardOutput;
            for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                line = line.Trim();
                if (line.StartsWith(“192“))
                {
                    string IP = line.Substring(0 15).Trim();
                    string Mac = line.Substring(line.IndexOf(“-“) - 2 0x11).Trim();
                    LocalMachine localMachine = new LocalMachine();
                    localMachine.MachineIP = IP;
                    localMachine.MachineMAC = Mac;
                    if (IP.Equals(“192.168.0.1“)) continue;
                    //try
                    //{
                    //    localMachine.MachineName = Dns.GetHostEntry(IP).HostName;
                    //}
                    //catch
                    //{
                    //    localMachine.MachineName = ““;
                    //    list.Add(localMachine);
                    //    continue;
                    //}
                    list.Add(localMachine);
                }
            }
            return list;
        }


        private void button1_Click(object sender EventArgs e)
        {
            textBox1.Text = “刷新列表“;
            ArrayList ththth = GetAllLocalMachines();
            if (ththth.Count == 0) return;
            textBox1.Text = ““;
            foreach (LocalMachine machine in ththth)
            {
                string s = string.Format(“{0}     {1}      {2}“ machine.MachineIP machine.MachineMAC machine.MachineName);
                textBox1.Text = string.Format(“{0}{1}\r\n“ textBox1.Text s);
            }
        }
        
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10752  2013-02-02 16:53  FindNetDevices\findDevice\bin\Debug\findDevice.exe

     文件      24064  2013-02-02 16:53  FindNetDevices\findDevice\bin\Debug\findDevice.pdb

     文件      11600  2013-02-02 17:09  FindNetDevices\findDevice\bin\Debug\findDevice.vshost.exe

     文件        490  2010-03-17 22:39  FindNetDevices\findDevice\bin\Debug\findDevice.vshost.exe.manifest

     文件       3679  2013-02-02 15:30  FindNetDevices\findDevice\findDevice.csproj

     文件       2984  2013-02-02 17:33  FindNetDevices\findDevice\Form1.cs

     文件       2558  2013-02-02 16:11  FindNetDevices\findDevice\Form1.Designer.cs

     文件       5817  2013-02-02 16:11  FindNetDevices\findDevice\Form1.resx

     文件       2152  2013-02-02 15:27  FindNetDevices\findDevice\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6199  2013-02-02 16:53  FindNetDevices\findDevice\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        652  2013-02-02 17:09  FindNetDevices\findDevice\obj\x86\Debug\findDevice.csproj.FileListAbsolute.txt

     文件      10752  2013-02-02 16:53  FindNetDevices\findDevice\obj\x86\Debug\findDevice.exe

     文件        180  2013-02-02 16:15  FindNetDevices\findDevice\obj\x86\Debug\findDevice.Form1.resources

     文件      24064  2013-02-02 16:53  FindNetDevices\findDevice\obj\x86\Debug\findDevice.pdb

     文件        180  2013-02-02 15:30  FindNetDevices\findDevice\obj\x86\Debug\findDevice.Properties.Resources.resources

     文件        220  2013-02-02 16:15  FindNetDevices\findDevice\obj\x86\Debug\GenerateResource.read.1.tlog

     文件        550  2013-02-02 16:15  FindNetDevices\findDevice\obj\x86\Debug\GenerateResource.write.1.tlog

     文件        491  2013-02-02 16:15  FindNetDevices\findDevice\Program.cs

     文件       1370  2013-02-02 15:27  FindNetDevices\findDevice\Properties\AssemblyInfo.cs

     文件       2870  2013-02-02 15:27  FindNetDevices\findDevice\Properties\Resources.Designer.cs

     文件       5612  2013-02-02 15:27  FindNetDevices\findDevice\Properties\Resources.resx

     文件       1095  2013-02-02 15:27  FindNetDevices\findDevice\Properties\Settings.Designer.cs

     文件        249  2013-02-02 15:27  FindNetDevices\findDevice\Properties\Settings.settings

     文件        872  2013-02-02 15:30  FindNetDevices\FindNetDevices.sln

    ..A..H.     20992  2013-02-02 17:09  FindNetDevices\FindNetDevices.suo

     目录          0  2013-02-02 15:27  FindNetDevices\findDevice\obj\x86\Debug\TempPE

     目录          0  2013-02-02 16:53  FindNetDevices\findDevice\obj\x86\Debug

     目录          0  2013-02-02 15:30  FindNetDevices\findDevice\bin\Debug

     目录          0  2013-02-02 15:27  FindNetDevices\findDevice\obj\x86

     目录          0  2013-02-02 15:27  FindNetDevices\findDevice\bin

............此处省略8个文件信息

评论

共有 条评论