gpt4 book ai didi

c# - 使用 C# 列出 Windows 用户帐户

转载 作者:可可西里 更新时间:2023-11-01 11:40:55 25 4
gpt4 key购买 nike

我必须列出所有用户(本地系统和域)。我尝试使用 WQL,但程序运行需要很长时间。还有其他方法可以从注册表中获取它吗?任何帮助将不胜感激。

最佳答案

using System;
using System.Collections.Generic;
using System.DirectoryServices;
namespace ListADUsers.ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
IList<String> userList = new List<String>();
int badEntries = 0;
string domainName = String.Empty;
if (args.Length > 0)
domainName = args[0];
else
{
Console.Write("\nPlease enter your Active Directory domain name: ");
domainName = Console.ReadLine();
}
Console.Write(String.Format("\nAttempting to build user list for {0} ...\n\n", domainName));
try
{
if (!String.IsNullOrEmpty(domainName))
{
DirectoryEntry myDirectoryEntry = new DirectoryEntry(String.Format("LDAP://{0}", domainName));
DirectorySearcher mySearcher = new DirectorySearcher(myDirectoryEntry);
SortOption mySort = new SortOption("sn", SortDirection.Ascending);
mySearcher.Filter = ("(objectCategory=person)");
mySearcher.Sort = mySort;
foreach (SearchResult resEnt in mySearcher.FindAll())
{
try
{
if (!String.IsNullOrEmpty(resEnt.Properties["Mail"][0].ToString())
&& System.Text.RegularExpressions.Regex.IsMatch(resEnt.Properties["DisplayName"][0].ToString(), " |admin|test|service|system|[$]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
)
{
int space = resEnt.Properties["DisplayName"][0].ToString().IndexOf(" ");
string formattedName = String.Format("{0}{1}{2}",
resEnt.Properties["DisplayName"][0].ToString().Substring(space).PadRight(25),
resEnt.Properties["DisplayName"][0].ToString().Substring(0, space).PadRight(15),
resEnt.Properties["Mail"][0].ToString()
);
userList.Add(formattedName);
}
}
catch
{
badEntries++;
}
}
if (userList.Count > 0)
{
Console.WriteLine(String.Format("=========== Listing of users in the {0} domain\n", domainName));
Console.WriteLine(String.Format("{0}{1}{2}", "Surname".PadRight(25), "First Name".PadRight(15), "Email Address\n"));
for (int i = 0; i < userList.Count - 1; i++)
Console.WriteLine(userList[i].ToString());
Console.WriteLine(String.Format("\n=========== {0} users found in the {1} domain", userList.Count.ToString(), domainName));
}
else
Console.WriteLine(String.Format("\n=========== 0 users found in the {0} domain", userList.Count.ToString()));
Console.WriteLine(String.Format("=========== {0} objects could not be read", badEntries.ToString()));
Console.WriteLine("=========== End of Listing");
}
else
{
Console.WriteLine("Please enter a domain name next time!");
}
}
catch (Exception ex)
{
// in a production app you wouldn't show the user the exception details
Console.Write(String.Format("A critical error occurred.\nDetails: {0}", ex.Message.ToString()));
}
}
}
}

下载示例应用程序:Sample Application to List AD Users

来源:Follow Up – List active directory users – This time in C#

关于c# - 使用 C# 列出 Windows 用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1502824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com