是一个删除系统账号,删除系统组的问题PrincipalContext MyComputer=new PrincipalContext(ContextType.Machine);
UserPrincipal usr=new UserPrincipal(MyComputer);
usr.SamAccountName="admin";
usr.SetPassword("admin");
usr.Save();
usr.Dispose();
这是简单的完成一个账号的添加
但是删除,不知如何编号?一直没找到方法
不能使用
usr.Delete();但方法确实是这样的,只是
UserPrincipal usr=new UserPrincipal(MyComputer);
这个地方不能这么写,有研究过这个的吗?

解决方案 »

  1.   

    System.DirectoryServices.AccountManagement去MSDN看下吧~!
      

  2.   

    我的怎么没有System.DirectoryServices.AccountManagement这个啊?
    我的是VS2005,你们用的是哪个版本啊? 
      

  3.   

    我用的是VS2008,VS2005有没有,我不知道,但我知道用.net2.0版本肯定是没有的,只有.net3.5才会有这个类库,使用时需引用System.DriectoryServices.AccountManagement我已自己解决,在此我发下代码,给遇到同样问题的朋友少走一些弯路
    以下是程序中其中一段代码:
            //删除账号
            private void User_del()
            {
                PrincipalContext MyComputer = new PrincipalContext(ContextType.Machine);
                try
                {
                    SqlConnection conn = new SqlConnection(Hit.Config.DBConn.SqlConn);
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = @"select * from [Config]";
                    cmd.CommandType = CommandType.Text;
                    conn.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    bool b = reader.Read();
                    if (b)
                    {
                        Hit.Config.Config.usr = "正在删除账号...";
                        UI_install_log();
                        UserPrincipal usr = UserPrincipal.FindByIdentity(MyComputer,reader["User_name"].ToString());
                        if (usr == null)
                        {
                            Hit.Config.Config.usr = "账号不存在,有可能同步未完成导致,请手动删除";
                            UI_install_log();
                        }
                        else
                        {
                            usr.Delete();
                            usr.Dispose();
                            Hit.Config.Config.usr = "账号删除成功!";
                            UI_install_log();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("读取数据库出错::错误代码506", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }