Find the Computers for this User Account

The application creates a new computer principal object to use as the search filter on the PrincipalSearcher object. The first computer principal that is found by the FindOne method is returned.

        private static ComputerPrincipal FindTheComputer(string computer)
        {
            // Creating the PrincipalContext
            PrincipalContext principalContext = null;
            try
            {
                principalContext = new PrincipalContext(ContextType.Domain, "fabrikam", "DC=fabrikam,DC=com");
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to create PrincipalContext. Exception: " + e);
                Application.Exit();
            }

            ComputerPrincipal queryFilter = new ComputerPrincipal(principalContext);
            queryFilter.SamAccountName = computer;
            PrincipalSearcher searcher = new PrincipalSearcher(queryFilter);
            ComputerPrincipal computerPrincipal = (ComputerPrincipal) searcher.FindOne();

            return computerPrincipal;
        }

See Also

Reference

System.DirectoryServices.AccountManagement

Concepts

About System.DirectoryServices.AccountManagement
Using System.DirectoryServices.AccountManagement

Send comments about this topic to Microsoft.

Copyright © 2008 by Microsoft Corporation. All rights reserved.