c# 获取当前系统中所有用户信息

yitian130 2007-05-02 03:48:46
如题~!

...全文
615 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yitian130 2007-05-02
  • 打赏
  • 举报
回复
结帖了~!

王集鹄 2007-05-02
  • 打赏
  • 举报
回复 1
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);
}
}
yitian130 2007-05-02
  • 打赏
  • 举报
回复
up

111,095

社区成员

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

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

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