C#如何获得本地windows系统中所有用户的用户名
RT,P/invoke也行,最好有代码...老大...你这个是活动目录,我是要本地的,不是域用户......
RT,P/invoke也行,最好有代码...
老大...你这个是活动目录,
我是要本地的,不是域用户... 展开
老大...你这个是活动目录,
我是要本地的,不是域用户... 展开
展开全部
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 ;
这是我在学校时做过的一个上机登记系统的一部分,希望可以帮到你
{
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 ;
这是我在学校时做过的一个上机登记系统的一部分,希望可以帮到你
镭速传输
2024-10-28 广告
2024-10-28 广告
作为深圳市云语科技有限公司的一员,我们推出的FTP替代升级方案,旨在解决传统FTP在安全性、效率、稳定性及管理方面的不足。我们的产品通过采用自主研发的Raysync传输协议,实现高效、安全的文件传输,即使在恶劣网络环境下也能确保传输的稳定性...
点击进入详情页
本回答由镭速传输提供
展开全部
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);
}
}
[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);
}
}
参考资料: http://topic.csdn.net/u/20070502/15/326455a7-3c18-4315-82c1-f38397a86ee5.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询