用C#中的windows用户名填充lisbox [英] populating lisbox with windows usernames in C#

查看:64
本文介绍了用C#中的windows用户名填充lisbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a listbox in C# winforms, and I want to populate it with all of local users in the system.
For some reason the Code I have doesnt work



继承人我现在拥有的东西


Heres What I have now

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
            DirectoryEntry admGroup = localMachine.Children.Find("users", "group");
            object members = admGroup.Invoke("members", null);
            foreach (object groupMember in (IEnumerable)members)
            {
                DirectoryEntry member = new DirectoryEntry(groupMember);
                lstUsers.Items.Add(member.Name);
            }

推荐答案

导入管理类

Import the management class
using System.Management;



将用户列表放入列表框


Get the users list into the listbox

SelectQuery query = new SelectQuery("Win32_UserAccount");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);


foreach (ManagementObject envVar in searcher.Get())
{
     lstUsers.Items.Add(envVar["Name"]);
}




I am not using my pc and its VS08 and i have to add a reference to the System.management so if it isn't working for you just add the reference





哦,可能有机会它还为您提供网络中的所有用户...



Oh and there might be a chance that its also giving you all the users in the network...


这篇关于用C#中的windows用户名填充lisbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆