Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C语言列出windows用户帐户#_C#_Windows_Wql - Fatal编程技术网

C# 使用C语言列出windows用户帐户#

C# 使用C语言列出windows用户帐户#,c#,windows,wql,C#,Windows,Wql,我必须列出所有用户(本地系统和域)。我尝试使用WQL,但运行程序需要很多时间。有没有其他方法可以从注册表中获取它?如有任何帮助,将不胜感激。使用系统; using System; using System.Collections.Generic; using System.DirectoryServices; namespace ListADUsers.ConsoleApp { class Program { static void Main(string[] a

我必须列出所有用户(本地系统和域)。我尝试使用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()));
            }
        }
    }
}
使用System.Collections.Generic; 使用System.DirectoryServices; 名称空间ListADUsers.ConsoleApp { 班级计划 { 静态void Main(字符串[]参数) { Console.Clear(); IList userList=新列表(); int=0; string domainName=string.Empty; 如果(args.Length>0) domainName=args[0]; 其他的 { 控制台。写(“\n请输入您的Active Directory域名:”); domainName=Console.ReadLine(); } Write(String.Format(“\n试图为{0}建立用户列表…\n\n”,域名)); 尝试 { 如果(!String.IsNullOrEmpty(域名)) { DirectoryEntry myDirectoryEntry=新的DirectoryEntry(String.Format(“LDAP://{0}”,domainName)); DirectorySearcher mySearcher=新的DirectorySearcher(myDirectoryEntry); SortOption mySort=新的SortOption(“sn”,SortDirection.升序); mysearch.Filter=(“(objectCategory=person)”; mySearcher.Sort=mySort; foreach(在mySearcher.FindAll()中重新发送搜索结果) { 尝试 { 如果(!String.IsNullOrEmpty(resEnt.Properties[“Mail”][0].ToString()) &&System.Text.RegularExpressions.Regex.IsMatch(resEnt.Properties[“DisplayName”][0].ToString(),“|管理|测试|服务|系统|[$]”,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(空格).PadRight(25), resEnt.Properties[“DisplayName”][0].ToString().Substring(0,空格).PadRight(15), resEnt.Properties[“邮件”][0].ToString() ); 添加(格式化名称); } } 抓住 { badEntries++; } } 如果(userList.Count>0) { Console.WriteLine(String.Format(“======================在{0}域\n”,domainName)中的用户列表); Console.WriteLine(String.Format(“{0}{1}{2}”,“姓氏”.PadRight(25),“名字”.PadRight(15),”电子邮件地址“); for(inti=0;i
下载示例应用程序:

资料来源: