private void GroupInfo()
{
DirectoryEntry MainGroup=new DirectoryEntry("WinNT:");
foreach(DirectoryEntry domain in MainGroup.Children)
{
listBox1.Text="";
listBox1.Items.Add(domain.Name);
}
}
private void ComputerInfo(string strname)
{
DirectoryEntry MainGroup=new DirectoryEntry("WinNT:");
foreach(DirectoryEntry domain in MainGroup.Children)
{
if(domain.Name==strname)
{
foreach(DirectoryEntry pc in domain.Children)
{
if(pc.Name!="Schema")//schema是结束标记
//this.listBox2.Items.Add(pc.Name);
{
try
{
DataRow dr=dt.NewRow();
dr[0]=pc.Name ;
IPHostEntry hostent = Dns.GetHostByName(pc.Name); // 主机信息
Array addrs = hostent.AddressList; // IP地址数组
IEnumerator it = addrs.GetEnumerator(); // 迭代器
while(it.MoveNext())
{ // 循环到下一个IP 地址
IPAddress ip = (IPAddress)it.Current; // 获得 IP 地址
dr[1]=ip.ToString(); // 显示 IP地址
dr[2]=ReadMac(ip.ToString());
}
dt.Rows.Add(dr);
}
catch
{
}
}
}
}
}
dataGrid1.DataSource =dt.DefaultView ;
}
private void 查看工作组计算机_Load(object sender, System.EventArgs e)
{
dt.Columns.Add(new DataColumn("主机名"));
dt.Columns.Add(new DataColumn("IP地址"));
dt.Columns.Add(new DataColumn("MAC地址"));
GroupInfo();
}
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
MessageBox.Show("开始检索请稍等…");
ComputerInfo(this.listBox1.Text);
MessageBox.Show("检索完成!");
}
private void button1_Click(object sender, System.EventArgs e)
{
ComputerInfo(this.listBox1.Text);
}
#region 获取MAC地址
public static string ReadMac(string ip)//传递IP地址,即可返回MAC地址
{
string mac = "";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "nbtstat";
p.StartInfo.Arguments = "-a " + ip;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
int len = output.IndexOf("MAC Address = ");
if(len>0)
{
mac = output.Substring(len + 14, 17);
}
p.WaitForExit();
return mac;
}
#endregion
C#的代码,大概是这样,添加了一个listBox等等,你应该看的懂吧
还得添加一些引用
全部引用:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.DirectoryServices;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using System.Net;
using System.Data ;
这是我在学校时做过的一个上机登记系统的一部分,希望可以帮到你
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct USER_INFO_0
{
public string Username;
}
[DllImport( "Netapi32.dll ")]
extern static int NetUserEnum(
[MarshalAs(UnmanagedType.LPWStr)]
string servername,
int level,
int filter,
out IntPtr bufptr,
int prefmaxlen,
out int entriesread,
out int totalentries,
out int resume_handle);
[DllImport( "Netapi32.dll ")]
extern static int NetApiBufferFree(IntPtr Buffer);
private void button1_Click(object sender, EventArgs e)
{
int EntriesRead;
int TotalEntries;
int Resume;
IntPtr bufPtr;
NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead,
out TotalEntries, out Resume);
if (EntriesRead > 0)
{
USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
IntPtr iter = bufPtr;
for (int i = 0; i < EntriesRead; i++)
{
Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
typeof(USER_INFO_0));
iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
textBox1.AppendText(Users[i].Username + "\r\n ");
}
NetApiBufferFree(bufPtr);
}
}