次の方法で共有


Delete the User Account

The FindByIdentity overload method on the user principal object locates the user object of the user account which will be deleted. The user account is then deleted by calling the Delete method.

        internal static bool DeleteTheAccount(string user)
        {
            // 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();
            }

            //Deleting the user account
            bool bReturn = false;
            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, user);

            if (userPrincipal != null)
            {                                
                try
                {
                    userPrincipal.Delete();
                    bReturn = true;
                }
                catch (Exception e)
                {
                    MessageBox.Show("Exception when deleting the account. " + e);
                }
            }

            return bReturn;
        }  

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.