Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8440611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:20:12+00:00 2026-06-10T08:20:12+00:00

Hello I’m stuck trying to add a function to my Windows forms program that

  • 0

Hello I’m stuck trying to add a function to my Windows forms program that allows a user to type in a textbox what computer or computers they would like to search for in Active Directory. The user would input the search string in a textbox then hit a button and the computers that match that search result would appear in a separate search box. Here is my code so far.

I would also like each computer name to be on a separate line such as:

computername1            
computername2        
computername3

Thanks!

This is what inside the button looks like:

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:20:13+00:00Added an answer on June 10, 2026 at 8:20 am

    If you’re on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:

    • Managing Directory Security Principals in the .NET Framework 3.5
    • MSDN docs on the System.DirectoryServices.AccountManagement namespace

    Basically, you can define a domain context and easily find users and/or groups in AD:

    // 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....     
       }
    }    
    

    If you don’t need to find a single computer, but search for a whole list of computers, you can use the new PrincipalSearcher interface, which basically allows you to set up a "QBE" (query-by-example) object you’re looking for, defining the search criteria, and then search for matches for those criteria.

    The new S.DS.AM namespace makes it really easy to play around with users and groups in AD!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello friends i am trying to get a list of user id and want
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
. Hello, I am trying to add a UIButton and other items to my
Hello I have like this 2 tables class User public int UserId{get;set;} { ....
Hello everyone i am trying to format the input number range with php number_format
Hello guys I need to pass a string array over to a setter that
Hello Guys I am trying to figure out why i am gettings this error
Hello i have a TextField on my scene. It haves only digits, user input
Hello i have this code var queue = new BlockingCollection<int>(); queue.Add(0); var producers =
Hello All I am trying to flatten a list in Ocaml. I am a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.