C#写的对系统用户和组操作的类(可以实现用户和组的添加、删除、修改)

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace SystemUserOperationDll
{
    public class SystemUserOperationClass
    {        
        private DirectoryEntry AD;
        

        /// <summary>
        /// 初始化方法
        /// </summary>
        /// <param name="strMachineName">机器名</param>
        /// <param name="strLoginName">登录用户名(如:Administrator)</param>
        /// <param name="strLoginPassword">登录密码</param>
        public SystemUserOperationClass(string strMachineName, string strLoginName, string strLoginPassword)
        {
            AD = new DirectoryEntry("WinNT://" + strMachineName + ",computer", ".\\" + strLoginName, strLoginPassword);
        }
        /// <summary>
        /// 添加系统(域)用户
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool AddDomainUser(string strUserName, string strPassword, string strDescription)
        {
            try
            {
                DirectoryEntry NewUser;
                NewUser = AD.Children.Add(strUserName, "user"); //添加用户
                NewUser.Invoke("SetPassword", new Object[] { strPassword });  //设置密码
                NewUser.Properties["description"].Add(strDescription);  //添加描述
                NewUser.CommitChanges();

                AD.Close();

            }
            catch
            {
                throw;
            }
            return true;


        }
        /// <summary>
        /// 修改系统(域)用户
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <param name="strPassword">密码</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool ModifyDomainUser(string strUserName, string strPassword, string strDescription)
        {
            try
            {
                DirectoryEntry User = AD.Children.Find(strUserName);
                if (User.Name != null)
                {
                    //修改密码
                    User.Invoke("SetPassword", new Object[] { strPassword });
                    //修改描述
                    User.Properties["description"].Value = strDescription;
                    User.CommitChanges();
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 删除系统(域)用户
        /// </summary>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public bool DeleteDomainUser(string strUserName)
        {
            try
            {
                DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到要删除的用户
                if (User.Name != null)
                {
                    AD.Children.Remove(User);
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 添加系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool AddDomainGroup(string strGroupName, string strDescription)
        {
            try
            {
                DirectoryEntry Group;
                Group = AD.Children.Add(strGroupName, "group");
                Group.Properties["description"].Add(strDescription);
                Group.CommitChanges();
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 修改系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strDescription">描述</param>
        /// <returns></returns>
        public bool ModifyDomainGroup(string strGroupName, string strDescription)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName);
                if (Group.Name != null)
                {
                    Group.Properties["description"].Value = strDescription;
                    Group.CommitChanges();
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 删除系统(域)组
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <returns></returns>
        public bool DeleteDomainGroup(string strGroupName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");
                if (Group.Name != null)
                {
                    AD.Children.Remove(Group);
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 添加组用户
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public bool AddGroupUser(string strGroupName,string strUserName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到组
                DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到用户
                if (Group.Name != null && User.Name != null)
                {
                    Group.Invoke("Add", new Object[] { User.Path });
                }
                AD.Close();
            }
            catch
            {
                throw;
            }
            return true;
        }
        /// <summary>
        /// 移除组用户
        /// </summary>
        /// <param name="strGroupName">组名</param>
        /// <param name="strUserName">用户名</param>
        /// <returns></returns>
        public bool RemoveGroupUser(string strGroupName,string strUserName)
        {
            try
            {
                DirectoryEntry Group = AD.Children.Find(strGroupName, "group");// 找到组
               
                object members = Group.Invoke("Members", null);
                foreach (object member in (System.Collections.IEnumerable)members)
                {
                    //获取该组的每个成员
                    DirectoryEntry x = new DirectoryEntry(member);

                    if (strUserName == x.Name) //要移除的用户存在的话,则从该组中移除。
                    {
                        DirectoryEntry User = AD.Children.Find(strUserName, "user");//找到该用户
                        Group.Invoke("Remove", new Object[] { User.Path });
                    }
                }
                AD.Close();               
            }
            catch
            {
                throw;
            }
            return true;
        }
              
    }

 

第一次做这方面的东西,有不足之处还请不吝指教。

posted on 2008-11-20 08:44  xiaohu  阅读(1195)  评论(1编辑  收藏  举报