C# 修改工作组内Window2003密码及帐号维护

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace ChangePwdWebpart
{
    class OperateDirectory
    {
        //修改密码
        public static bool ChangePwd(string UserName, string oldPwd, string newPwd)
        {
            try
            {
                DirectoryEntry MachineDirectoryEntry;
                MachineDirectoryEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName);
                DirectoryEntry CurrentDirectoryEntry = MachineDirectoryEntry.Children.Find(UserName);
                CurrentDirectoryEntry.Invoke("ChangePassword", new Object[] { oldPwd, newPwd });
                CurrentDirectoryEntry.CommitChanges();
                CurrentDirectoryEntry.Close();
                return true;      
            }
            catch (Exception exp)
            {
                if (exp.InnerException.Message.Replace("'", "").IndexOf("网络密码不正确") != -1)
                throw new Exception("密码修改失败,输入的原始密码不正确");
                else
                throw new Exception(exp.InnerException.Message);
            }
            finally
            {

            }
        }

         //新增组:
        private void AddGroup()
        {
            string path = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry entryPC = new DirectoryEntry(path);
            DirectoryEntry newEntry = entryPC.Children.Add("NewGroup", "Group");
            //newEntry.Properties["groupType"][0] = "4";
            newEntry.Properties["Description"].Add("test");
            newEntry.CommitChanges();
        }
        //删除组:
        private void DelGroup()
        {
            string userGroup = "NewGroup";
            string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry parent = new DirectoryEntry(path1);
            object[] paras = new object[2];
            paras[0] = "group";
            paras[1] = userGroup;
            parent.Invoke("Delete", paras);
        }
        //查找组:
        private void FindGroup()
        {
            string userGroup = "NewGroup";
            string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry parent = new DirectoryEntry(path1);
            DirectoryEntry group = parent.Children.Find(userGroup, "group");
            if (group != null)
                MessageBox.Show("Group find.");
            else
                MessageBox.Show("Group not found.");
        }
        //新增用户:
        private void AddUser()
        {
            string path = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry entryPC = new DirectoryEntry(path);
            DirectoryEntry obUser = entryPC.Children.Add("NewUser", "User");
            obUser.Properties["Description"].Add("Test User from .NET");
            obUser.Properties["FullName"].Add("NewUser");
            object obRet = obUser.Invoke("SetPassword", "123");
            obUser.CommitChanges();
        }
        //删除用户:
        private void Deluser()
        {
            string userName = "NewUser";
            string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry parent = new DirectoryEntry(path1);
            object[] paras = new object[2];
            paras[0] = "user";
            paras[1] = userName;
            parent.Invoke("Delete", paras);
        }
        //查找用户:
        private void findUser()
        {
            string userName = "NewUser";
            string path1 = String.Format("WinNT://{0}", System.Environment.MachineName);
            DirectoryEntry parent = new DirectoryEntry(path1);
            DirectoryEntry user = parent.Children.Find(userName, "user");
            if (user != null)
                MessageBox.Show("User find.");
            else
                MessageBox.Show("User not found.");
        }

    }
}

你可能感兴趣的:(window)