使用 C# 获取 Windows 用户

发布于 2024-11-07 23:43:33 字数 47 浏览 0 评论 0 原文

如何使用 .NET (C#) 获取本地计算机的所有 Windows 用户的列表?

How can I get a list of all windows users of the local machine with the usage of .NET (C#) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2024-11-14 23:43:33

这是一篇博客文章(带有代码),解释了如何执行此操作:

http://csharptuning.blogspot.com/2007/09/how-to-get-list-of-windows-user-in-c.html

作者列出了以下代码(引自上述网站):

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);
}

您需要在代码顶部添加 using System.DirectoryServices 。要更改计算机,您可以将 Environment.MachineName 更改为您想要访问的任何计算机(只要您有权这样做并且防火墙不阻止您这样做)。我还修改了作者的代码以查看 users 组而不是 administrators 组。

Here is a blog post (with code) that explains how to do it:

http://csharptuning.blogspot.com/2007/09/how-to-get-list-of-windows-user-in-c.html

The author lists the following code (quoted from the above site):

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);
}

You need to add using System.DirectoryServices at the top of your code. To change machines, you would change the Environment.MachineName to be whatever machine you want to access (as long as you have permission to do so and the firewall isn't blocking you from doing so). I also modified the author's code to look at the users group instead of the administrators group.

一腔孤↑勇 2024-11-14 23:43:33

这取决于您真正“追求”的是什么...如果您位于 Windows 域(使用 Active Directory),那么您可以查询 Active Directory IF Active Directory 用于限制“授权”使用本地计算机的用户。

如果您的要求不是那么严格,那么您可以检查系统 UserProfiles 中的文件夹,其中除“默认用户”和“所有用户”之外的每个文件夹都代表已登录到本地计算机的用户配置文件。 警告这可能包括系统和/或服务帐户...

It depends on what you are really 'after'... if you are on a windows domain (using active directory) then you can query Active Directory IF active directory is being used to limit the users who are "authorized" to use the local machine.

If your requirements are not as stringent then you can inspect the folders in the system UserProfiles where each folder except Default User and All Users represent a user profile that has logged into the local machine. caution this may include system and/or service accounts...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文