收藏 分享(赏)

C#获取域用户列表.doc

上传人:HR专家 文档编号:6999072 上传时间:2019-04-30 格式:DOC 页数:3 大小:28KB
下载 相关 举报
C#获取域用户列表.doc_第1页
第1页 / 共3页
C#获取域用户列表.doc_第2页
第2页 / 共3页
C#获取域用户列表.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

1、private string RunCmd(string command)/實例一個 Process 類,啟動一個獨立進程Process p = new Process();/Process 類有一個 StartInfo 屬性,這個是 ProcessStartInfo 類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:p.StartInfo.FileName = “cmd.exe“; /設定程序名p.StartInfo.Arguments = “/c “ + command; /設定程式執行參數p.StartInfo.UseShellExecute = false; /關閉 Shell 的

2、使用p.StartInfo.RedirectStandardInput = true; /重定向標準輸入p.StartInfo.RedirectStandardOutput = true; /重定向標準輸出p.StartInfo.RedirectStandardError = true; /重定向錯誤輸出p.StartInfo.CreateNoWindow = true; /設置不顯示窗口p.Start(); /啟動/p.StandardInput.WriteLine(command); /也可以用這種方式輸入要執行的命令/p.StandardInput.WriteLine(“exit“);

3、/不過要記得加上 Exit 要不然下一行程式執行的時候會當機return p.StandardOutput.ReadToEnd(); /從輸出流取得命令執行結果private static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)DirectorySearcher searcher = new DirectorySearcher();searcher.SearchRoot = new Direct

4、oryEntry(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 searchsearcher.PageS

5、ize = 512;searcher.PropertiesToLoad.AddRange(new string “name“, “Path“, “displayname“, “samaccountname“, “mail“ );SearchResultCollection results = searcher.FindAll();return results;/参数 domainADsPath 是活动目录的域名,使用类似“LDAP:/域名“的形式/参数 schemaClassNameToSearch 是过滤条件,/ objectClass=user 查询条件是所有的用户(USER)public

6、 GetUserList() public string ListUsers()string path = “LDAP:/IP/CN=Users,DC=idm,DC=gad,DC=nec,DC=com,DC=cn“;/ IP:ADIP 地址/ DC:域例如 ,cn 可以写为 DC=sina,DC=com,DC=cn/ CN:数据对象 指定要获取的内容return ListUsers(path);public string ListUsers(string path)tryDirectoryEntry entry = new DirectoryEntry(path);DirectorySearc

7、her searcher = new DirectorySearcher(entry);searcher.Filter = “(objectClass=*)“;searcher.PropertiesToLoad.Clear();SearchResultCollection searchResultCollection = searcher.FindAll();return VisitSearchResultCollection(searchResultCollection);catch (Exception ex) log.Debug(ex.Message); return new strin

8、g 0; /string messageFormat = “key:0 value:1 desc:“;public void VisitSearchResultCollection(SearchResultCollection resultCollection)IList userList = new List();foreach(SearchResult result in resultCollection) string userName;string displayName;if (result.Properties.Contains(“samaccountname“) ResultPr

9、opertyValueCollection resultValue = result.Properties“samaccountname“;if(resultValue!= null if (result.Properties.Contains(“displayname“) ResultPropertyValueCollection resultValue = result.Properties“displayname“;if(resultValue!= null userList.Add(new Users(userName,displayName);UploadHHTHistory(userList);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报