让Windows用户使用C# [英] Get windows users with C#

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

问题描述

我怎样才能得到所有的Windows用户在本地计算机的列表与.NET(C#)?的使用

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

推荐答案

下面是一篇博客文章(与code),说明如何做到这一点:

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

<一个href="http://csharptuning.blogspot.com/2007/09/how-to-get-list-of-windows-user-in-c.html">http://csharptuning.blogspot.com/2007/09/how-to-get-list-of-windows-user-in-c.html

笔者列出了以下code(从上述网站引用):

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

您需要在您的code顶部添加使用System.DirectoryServices中。要更改机器上,你会改变 Environment.MachineName 是任何机器,你要访问(只要你有这样的权限和防火墙没有阻止你这样做)。我还修改了作者的code看看用户组而不是管理​​员

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.

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

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