所以,我有点卡在这里......
我正在编写一个程序,它应该能够列出 MS Windows Server 2008 R2 上本地管理员组中的所有用户。
这里的问题是我只被允许使用 .NET 2.0 - 所以我不能使用 GroupPrincipal 类......这会使这变得非常容易。
任何指针都会被应用!
干杯!
所以,我有点卡在这里......
我正在编写一个程序,它应该能够列出 MS Windows Server 2008 R2 上本地管理员组中的所有用户。
这里的问题是我只被允许使用 .NET 2.0 - 所以我不能使用 GroupPrincipal 类......这会使这变得非常容易。
任何指针都会被应用!
干杯!
天哪!
不知道我在想什么——太简单了!
对 Masoud Tabatabaei 的所有信任 - 在以下位置找到以下代码片段: http ://csharptuning.blogspot.se/2007/09/how-to-get-list-of-windows-user-in-c.html
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators","group");
object members = admGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
lstUsers.Items.Add(member.Name);
}
你试过 WMI 吗?
例如
ManagementObjectSearcher search = new ManagementObjectSearcher(@"SELECT * FROM Win32_UserAccount where LocalAccount = true");
ManagementObjectCollection userList = search.Get();
foreach (ManagementObject user in userList)
{
Console.WriteLine("User name: {0}, Full Name: {1}",
user["Name"].ToString(), user["FullName"].ToString());
}
将为您提供本地 SAM 中的用户列表。您可以将其他属性添加到查询并优化您的列表。
不要忘记添加对 System.Management.dll 的引用
If your are still looking for an answer, here:
If you'd like to get the administrator group, you can use this code:
public static DirectoryEntry GetLocalAdminstratorGroup()
{
using (var WindowsActiveDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
{
return WindowsActiveDirectory.Children.Find(GetLocalizedAdministratorGroupName(), "group");
}
}
//Localized == Language Independent
public static string GetLocalizedAdministratorGroupName()
{
//For English Windows version, this equals "BUILTIN\Administrators".
var adminGroupName = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null).Translate(typeof(NTAccount)).Value;
//Remove the "BUILTIN\" part, get the local name of the group
return adminGroupName.Split('\\')[1];
}
If you'd also like to enumerate it (like you need a username), you can do this, using the methods before:
object members = AdminGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
Console.WriteLine(member.Name);
}