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.");
        }

    }
}

转载于:https://www.cnblogs.com/flat_peach/archive/2008/07/07/1237226.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值