在网上发现用C#获取局域网内所有电脑IP及MAC 的代码, 但我在VS2008 C# winform 下发现出错:提示找不到类型或命名空间名称“LocalMachine”(是否缺少 using 指令或程序集引用?
烦请大家看看如何引用及指教,谢谢!
引用:http://hi.baidu.com/leoliu83/blog/item/42c3a099ca974d186f068cea.html
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.Collections;
using System.Net;
//using System.Security;
using System.Diagnostics;
using System.IO;
//using Microsoft.Win32.Registry; //这样会出错
//Microsoft.Win32.Registry.LocalMachine 有人回答是这个,但发现不可引用
using Microsoft.Win32;
using System.Net.NetworkInformation;
using System.DirectoryServices;
using System.Management;
using Microsoft.VisualBasic.Devices;
public 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;
string IPHead = Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString().Substring(0, 3);
for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{
line = line.Trim(); if (line.StartsWith(IPHead) && (line.IndexOf("dynamic") != -1))
{
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;
localMachine.MachineName = "";
list.Add(localMachine);
}
}
return list;
}