2012-08-28 4 views
5

안녕하세요 저는 사용자가 텍스트 상자에 어떤 컴퓨터를 검색 할 것인지를 입력 할 수있는 기능을 내 Windows 프로그램에 추가하려고했습니다. Active Directory. 사용자는 텍스트 상자에 검색 문자열을 입력 한 다음 단추를 누르면 해당 검색 결과와 일치하는 컴퓨터가 별도의 검색 상자에 나타납니다. 지금까지 제 코드가 있습니다.Active Directory에서 사용자 입력을 사용하여 컴퓨터 이름을 검색하십시오.

computername1    
computername2   
computername3 

감사 :

은 또한 각각의 컴퓨터 이름과 같은 별도의 행에 싶습니다!

이 버튼의 모양 안에 무엇을 : 당신이 .NET 3.5 및 최대에있는 경우

List<string> hosts = new List<string>(); 
DirectoryEntry de = new DirectoryEntry(); 
de.Path = "LDAP://servername"; 

try 
{ 
    string adser = txtAd.Text; //textbox user inputs computer to search for 

    DirectorySearcher ser = new DirectorySearcher(de); 
    ser.Filter = "(&(ObjectCategory=computer)(cn=" + adser + "))"; 
    ser.PropertiesToLoad.Add("name"); 

    SearchResultCollection results = ser.FindAll(); 

    foreach (SearchResult res in results) 
    //"CN=SGSVG007DC" 
    { 
     string computername = res.GetDirectoryEntry().Properties["Name"].Value.ToString(); 
     hosts.Add(computername); 
     //string[] temp = res.Path.Split(','); //temp[0] would contain the computer name ex: cn=computerName,.. 
     //string adcomp = (temp[0].Substring(10)); 
     //txtcomputers.Text = adcomp.ToString(); 
    } 

    txtcomputers.Text = hosts.ToString(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.ToString()); 
} 
finally 
{ 
    de.Dispose();//Clean up resources 
} 
+0

[이전 질문에 대한 답변을 수락해야합니다] (http://meta.stackexchange.com/q/5234/153998). 이것은 당신을 돕기 위해 자신의 시간을 보냈던 사람들에 대한 감사를 나타내며, 미래의 질문에 답할 수있는 기회를 향상시킵니다. –

답변

7

, 당신은 System.DirectoryServices.AccountManagement (S.DS.AM) 네임 스페이스를 확인해야합니다. 여기에 대한 모든 읽기 :

// set up domain context 
using(PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find a computer 
    ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(ctx, "SomeComputerName"); 

    if(computer != null) 
    { 
     // do something here....  
    } 
}  

경우 :

, 당신은 AD에서 사용자 및/또는 그룹을 쉽게 찾을 도메인 컨텍스트를 정의 할 수 있습니다 당신은 하나의 컴퓨터를 찾을 필요가 없지만 컴퓨터의 전체 목록을 검색하면 새로운 PrincipalSearcher 인터페이스를 사용할 수 있습니다. 찾는 QBE (query-by-example) 객체를 설정하고 검색 기준을 정의한 다음 해당 기준에 일치하는 항목을 검색해야합니다.

새로운 S.DS.AM은 광고에서 사용자 및 그룹과 놀기가 정말 쉽습니다!

+0

고마워, 아프다. – Boundinashes6

관련 문제