- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.DirectoryServices;
- using System.Diagnostics;
- using System.IO;
- public static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)
- {
- DirectorySearcher searcher = new DirectorySearcher();
- searcher.SearchRoot = new DirectoryEntry(domainADsPath, username, password);
- searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")";
- searcher.SearchScope = SearchScope.Subtree;
- searcher.Sort = new SortOption("name", SortDirection.Ascending);
- // If there is a large set to be return ser page size for a paged search
- searcher.PageSize = 512;
- searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "samaccountname", "mail" });
- if (searcher.FindAll() == null)
- {
- Exception e = new Exception();
- Console.WriteLine("搜索结果为空");
- throw e;
- }
- SearchResultCollection results = searcher.FindAll();
- return results;
- //参数domainADsPath是活动目录的域名,使用类似"LDAP://域名"的形式
- //参数schemaClassNameToSearch是过滤条件,
- // objectClass=user 查询条件是所有的用户(USER)
- }
- public static SearchResultCollection _ADHelper(string domainADsPath, string schemaClassNameToSearch)
- {
- DirectorySearcher searcher = new DirectorySearcher();
- searcher.SearchRoot = new DirectoryEntry(domainADsPath);
- searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")";
- searcher.SearchScope = SearchScope.Subtree;
- searcher.Sort = new SortOption("name", SortDirection.Ascending);
- // If there is a large set to be return ser page size for a paged search
- searcher.PageSize = 1024;
- searcher.PropertiesToLoad.AddRange(new string[] { "name", "physicaldeliveryofficename", "displayname", "samaccountname", "mail" });
- if (searcher.FindAll() == null)
- {
- Exception e = new Exception();
- Console.WriteLine("搜索结果为空");
- throw e;
- }
- SearchResultCollection results = searcher.FindAll();
- return results;
- //参数domainADsPath是活动目录的域名,使用类似"LDAP://域名"的形式
- //参数schemaClassNameToSearch是过滤条件,
- // objectClass=user 查询条件是所有的用户(USER)
- }
- public static string[] GetGetUserUnitResults(SearchResultCollection results)
- {
- string sRe = string.Empty;
- if (results.Count == 0)
- throw new Exception("域中没有任何用户 ");
- else
- {
- foreach (SearchResult result in results)
- {
- string ADPath = result.Path;
- string[] sSplit = ADPath.Split(',');
- string orz = string.Empty;
- if (sSplit.Length > 2)
- {
- orz = sSplit[0].Split('=')[1];
- }
- ResultPropertyCollection propColl = result.Properties;
- if (propColl["physicaldeliveryofficename"].Count > 0)
- orz += "\\t" + propColl["physicaldeliveryofficename"][0].ToString();
- sRe += orz + "= ";
- }
- }
- if (sRe.Length > 0)
- sRe = sRe.Substring(0, sRe.Length - 1);
- return sRe.Split('=');
- }
- public static void write2file(string[] s)
- {
- if (File.Exists("user.txt"))
- File.Delete("user.txt");
- FileStream fs = new FileStream("user.txt", FileMode.Create);
- StreamWriter sw = new StreamWriter(fs);
- for (int i = 0; i < s.Length; i++)
- {
- sw.WriteLine(s[i]);
- }
- sw.Close();
- fs.Close();
- }
- //该片段来自于http://www.codesnippet.cn/detail/1704201512292.html
来源: http://www.codesnippet.cn/detail/1704201512292.html