获取计算机的用户
我正在尝试使用以下代码获取计算机的本地用户列表。
internal void GetUsers()
{
try
{
List<string> adUsers = new List<string>();
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
foreach (DirectoryEntry child in directoryEntry.Children)
{
if (child.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase))
{
adUsers.Add(child.Name);
}
}
}
catch (Exception ex)
{
//Exception
}
}
这段代码在我的电脑上运行良好。然而,当我在其他几台计算机上进行测试时,列表中包含以下系统用户:
ASPNET、 HelpAssistant
有人可以告诉我如何摆脱这些系统用户并只获取实际登录的用户,即普通用户。
谢谢, 内存
I am trying to get the list of local users of a computer using the following code.
internal void GetUsers()
{
try
{
List<string> adUsers = new List<string>();
DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
foreach (DirectoryEntry child in directoryEntry.Children)
{
if (child.SchemaClassName.Equals("User", StringComparison.OrdinalIgnoreCase))
{
adUsers.Add(child.Name);
}
}
}
catch (Exception ex)
{
//Exception
}
}
This code works fine in my computer. However, when I tested it on a few other computers, the following system users were included in the list:
ASPNET,
HelpAssistant
Could some one throw some light on how I can get rid of these system users and get only users who actually log in, ie, normal users.
Thanks,
Ram
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不是一个答案,而是一些可能有帮助的建议。
我认为问题在于这些帐户不是真正的系统帐户,因此可能不太容易区分。
您可以查看 WMI 类 Win32_UserAccount和 Win32_UserProfile 并查看是否有其中的属性可能表明哪些用户帐户是普通帐户以及哪些是您提到的帐户。具体来说,可能是
Win32_UserAccount
的“SIDType”或“AccountType”属性,也可能是Win32_UserProfile
类的Special
属性。可能还有其他值得关注的 WMI 类。
或者可能有某种方法可以查询用户帐户是否具有交互式登录权限(我假设这两个帐户通常可能没有)。
Not an answer as such, but some suggestions that might help.
I think the problem is that those accounts aren't real system accounts, so might not be so easy to distinguish.
You could look at the WMI classes Win32_UserAccount and Win32_UserProfile and see if there are any properties in there that might indicate which user accounts are normal ones and which ones are the ones you mention. Specifically, maybe the 'SIDType' or 'AccountType' properties of
Win32_UserAccount
or maybe theSpecial
property of theWin32_UserProfile
class.Might be other WMI classes that might be worth looking at as well.
Or there might be some way that you can query if a user account has the interactive logon right (which I assume those two accounts might not have normally).
您是否尝试过枚举 DirectoryEntry 上的 Properties 集合?
除此之外,您可能需要在 Active Directory 上执行 LDAP 搜索,以将您找到的用户名与 ActiveDirectory 用户相匹配。
看看这篇文章。
http://www.codeproject.com/KB/system/everythingInAD.aspx
玩得开心。
Have you tried enumerating the Properties collection on DirectoryEntry?
Other than that, you may have to do an LDAP search on Active Directory to match the UserName you have found to an ActiveDirectory user.
Have a look at this article.
http://www.codeproject.com/KB/system/everythingInAD.aspx
Have fun.
以下代码将为您提供实际拥有本地可访问文件夹的本地用户。
The following code will get you the local users that actually have local accessible folders.