请教用C#获取局域网内所有电脑IP及MAC 的代码的LocalMachine引用空间

sz_xd 2011-09-25 01:54:40
在网上发现用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;
}

...全文
407 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sz_xd 2011-10-11
  • 打赏
  • 举报
回复

谢谢wknight_IT(風騎士之怒)的提示!
sz_xd 2011-09-25
  • 打赏
  • 举报
回复
谢谢wknight_IT的回复,原来是个自定义类呀!害我找了一晚呀!

public class LocalMachine
{
// Fields
private string machineIP;
private string machineMAC;
private string machineName;

// Methods
// public LocalMachine();

// Properties
public string MachineIP { get; set; }
public string MachineMAC { get; set; }
public string MachineName { get; set; }
}


但我加上那个自定义类后,发现实际出来的并不是局域网内的全部IP呀!

烦请大家再帮忙了!谢谢!
马老虎 2011-09-25
  • 打赏
  • 举报
回复
去百度下这个 LocalMachine 的命名空间 添加引用




老虎插件↓
快速添加:http://malaohu-project.googlecode.com/files/Plugin_v1.5.1.js
sz_xd 2011-09-25
  • 打赏
  • 举报
回复
现我已搞好并读出来了!他们两个贴出代码都是一段一段的,没齐全,给我新手加难道? 要再加一个显示出来就可以了!搞到我找了大半天才找到有关

private void timer1_Tick(object sender, EventArgs e) //我加了一个计时器定时刷新显示
{

listBox1.Items.Clear();

ArrayList arr = GetAllLocalMachines();

// listBox1.DataSource = arr; 原以为直接取,但这样不行

foreach (LocalMachine str in arr)
{
listBox1.Items.Add(str.MachineIP.ToString() + ">" + str.MachineMAC.ToString() + ">" + str.MachineName.ToString());
}


i1++;

HtextBox1.Text = "运行:第" + i1.ToString() + "次";




但发现这种方式还是不太好,我多次调作时发现不太稳定,有时显示有PC的IP,有时会没显示出来!
是否有更好方法的?
风骑士之怒 2011-09-25
  • 打赏
  • 举报
回复
蛋疼,这些转贴者不把文章转转完,害人啊

LocalMachine是一个自定义类而已

参考:http://blog.csdn.net/nnsword/article/details/2347485

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧