• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#反射实例发布时间:2022-07-10
下一篇:
转:c# ArrayList 的 Sort()方法的使用发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap