您好,欢迎访问三七文档
privatestringRunCmd(stringcommand){//實例一個Process類,啟動一個獨立進程Processp=newProcess();//Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,下面我們用到了他的幾個屬性:p.StartInfo.FileName=cmd.exe;//設定程序名p.StartInfo.Arguments=/c+command;//設定程式執行參數p.StartInfo.UseShellExecute=false;//關閉Shell的使用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);//不過要記得加上Exit要不然下一行程式執行的時候會當機returnp.StandardOutput.ReadToEnd();//從輸出流取得命令執行結果}privatestaticSearchResultCollection_ADHelper(stringdomainADsPath,stringusername,stringpassword,stringschemaClassNameToSearch){DirectorySearchersearcher=newDirectorySearcher();searcher.SearchRoot=newDirectoryEntry(domainADsPath,username,password);searcher.Filter=(objectClass=+schemaClassNameToSearch+);searcher.SearchScope=SearchScope.Subtree;searcher.Sort=newSortOption(name,SortDirection.Ascending);//Ifthereisalargesettobereturnserpagesizeforapagedsearchsearcher.PageSize=512;searcher.PropertiesToLoad.AddRange(newstring[]{name,Path,displayname,samaccountname,mail});SearchResultCollectionresults=searcher.FindAll();returnresults;//参数domainADsPath是活动目录的域名,使用类似LDAP://域名的形式//参数schemaClassNameToSearch是过滤条件,//objectClass=user查询条件是所有的用户(USER)}publicGetUserList(){}publicstring[]ListUsers(){stringpath=LDAP://IP/CN=Users,DC=idm,DC=gad,DC=nec,DC=com,DC=cn;//IP:ADIP地址//DC:域例如sina.com,cn可以写为DC=sina,DC=com,DC=cn//CN:数据对象指定要获取的内容returnListUsers(path);}publicstring[]ListUsers(stringpath){try{DirectoryEntryentry=newDirectoryEntry(path);DirectorySearchersearcher=newDirectorySearcher(entry);searcher.Filter=(objectClass=*);searcher.PropertiesToLoad.Clear();SearchResultCollectionsearchResultCollection=searcher.FindAll();returnVisitSearchResultCollection(searchResultCollection);}catch(Exceptionex){log.Debug(ex.Message);returnnewstring[0];}}//stringmessageFormat=key:{0}value:{1}desc:;publicvoidVisitSearchResultCollection(SearchResultCollectionresultCollection){IListUsersuserList=newListUsers();foreach(SearchResultresultinresultCollection){stringuserName;stringdisplayName;if(result.Properties.Contains(samaccountname)){ResultPropertyValueCollectionresultValue=result.Properties[samaccountname];if(resultValue!=null&&resultValue.Count0&&resultValue[0]!=null){userName=resultValue[0].ToString();}}if(result.Properties.Contains(displayname)){ResultPropertyValueCollectionresultValue=result.Properties[displayname];if(resultValue!=null&&resultValue.Count0&&resultValue[0]!=null){displayName=resultValue[0].ToString();}}userList.Add(newUsers(userName,displayName));}UploadHHTHistory(userList);}
本文标题:C#获取域用户列表
链接地址:https://www.777doc.com/doc-7027120 .html