得到一个WINNT组的成员名单 [英] Get a list of members of a WinNT group
问题描述
有几个问题类似于此的堆栈溢出,但不完全一样。
There are a couple of questions similar to this on stack overflow but not quite the same.
我想开了,或Win XP的计算机上创建一个本地组,成员添加到它,域,本地和众所周知的帐户。我还需要检查用户是否已经是一个成员,这样我不添加相同的帐户两次,presumably得到一个异常。
I want to open, or create, a local group on a win xp computer and add members to it, domain, local and well known accounts. I also want to check whether a user is already a member so that I don't add the same account twice, and presumably get an exception.
到目前为止,我开始使用与 WINNT的DirectoryEntry对象://
提供商。这是怎么回事确定,但我卡在如何让一组的成员名单?
So far I started using the DirectoryEntry object with the WinNT://
provider. This is going ok but I'm stuck on how to get a list of members of a group?
任何人都知道如何做到这一点?或提供一个更好的解决方案比使用的DirectoryEntry?
Anyone know how to do this? Or provide a better solution than using DirectoryEntry?
推荐答案
好了,它采取了一段时间,有不同的解决方案,但最适合下面我原来的问题给出一个插科打诨。我不能让DirectoryEntry对象使用标准的方法来访问本地组的成员,我可以得到它的枚举成员的唯一方法是通过使用Invoke方法来调用本机对象成员的方法。
Okay, it's taken a while, messing around with different solutions but the one that fits best with my original question is given below. I can't get the DirectoryEntry object to access the members of a local group using the 'standard' methods, the only way I could get it to enumerate the members was by using the Invoke method to call the native objects Members method.
using(DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group"))
{
foreach(object member in (IEnumerable) groupEntry.Invoke("Members"))
{
using(DirectoryEntry memberEntry = new DirectoryEntry(member))
{
Console.WriteLine(memberEntry.Path);
}
}
}
我也采用了类似的技术来添加,并从本地组删除成员。
I also used a similar technique to add and remove members from the local group.
我希望这可以帮助别人也是如此。 基思。
Hopefully this helps someone else as well. Keith.
修改添:加VB.Net版本
EDIT by Tim: added VB.Net version
Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryEntry)
Dim members As New List(Of DirectoryEntry)
Try
Using search As New DirectoryEntry("WinNT://./" & GroupName & ",group")
For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
Dim memberEntry As New DirectoryEntry(member)
members.Add(memberEntry)
Next
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return members
End Function
这篇关于得到一个WINNT组的成员名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!