在网上发现用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;
    }

解决方案 »

  1.   

    蛋疼,这些转贴者不把文章转转完,害人啊LocalMachine是一个自定义类而已参考:http://blog.csdn.net/nnsword/article/details/2347485
      

  2.   

    去百度下这个 LocalMachine 的命名空间 添加引用
      

  3.   

    谢谢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呀!烦请大家再帮忙了!谢谢!
      

  4.   

    现我已搞好并读出来了!他们两个贴出代码都是一段一段的,没齐全,给我新手加难道? 要再加一个显示出来就可以了!搞到我找了大半天才找到有关       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,有时会没显示出来!
    是否有更好方法的?
      

  5.   

     
    谢谢wknight_IT(風騎士之怒)的提示!