国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# 對(duì)windows用戶和組操作實(shí)例

2020-01-24 03:28:24
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace JH.Framework.Security
{
    ///

    /// 計(jì)算機(jī)用戶和組操作類
    ///

    public class UserAndGroupHelper
    {
        private static readonly string PATH = "WinNT://" + Environment.MachineName;
        ///

        /// 添加windows用戶
        ///

        /// 用戶名
        /// 密碼
        /// 所屬組
        /// 描述
        public static void AddUser(string username, string password, string group, string description)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry user = dir.Children.Add(username, "User")) //增加用戶名
                {
                    user.Properties["FullName"].Add(username); //用戶全稱
                    user.Invoke("SetPassword", password); //用戶密碼
                    user.Invoke("Put", "Description", description);//用戶詳細(xì)描述
                    //user.Invoke("Put","PasswordExpired",1); //用戶下次登錄需更改密碼
                    user.Invoke("Put", "UserFlags", 66049); //密碼永不過期
                    //user.Invoke("Put", "UserFlags", 0x0040);//用戶不能更改密碼s
                    user.CommitChanges();//保存用戶
                    using (DirectoryEntry grp = dir.Children.Find(group, "group"))
                    {
                        if (grp.Name != "")
                        {
                            grp.Invoke("Add", user.Path.ToString());//將用戶添加到某組
                        }
                    }
                }
            }
        }
        ///

        /// 更改windows用戶密碼
        ///

        /// 用戶名
        /// 新密碼
        public static void UpdateUserPassword(string username, string newpassword)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry user = dir.Children.Find(username, "user"))
                {
                    user.Invoke("SetPassword", new object[] { newpassword });
                    user.CommitChanges();
                }
            }
        }
        ///

        /// 刪除windows用戶
        ///

        /// 用戶名
        public static void RemoveUser(string username)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry user = dir.Children.Find(username, "User"))
                {
                    dir.Children.Remove(user);
                }
            }
        }
        ///

        /// 添加windows用戶組
        ///

        /// 組名稱
        /// 描述
        public static void AddGroup(string groupName, string description)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry group = dir.Children.Add(groupName, "group"))
                {
                    group.Invoke("Put", new object[] { "Description", description });
                    group.CommitChanges();
                }
            }
        }
        ///

        /// 刪除windows用戶組
        ///

        /// 組名稱
        public static void RemoveGroup(string groupName)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry group = dir.Children.Find(groupName, "Group"))
                {
                    dir.Children.Remove(group);
                }
            }
        }
    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新晃| 保德县| 汤阴县| 夏津县| 荆州市| 彰化县| 东乌| 卓资县| 晋宁县| 邹城市| 岳普湖县| 班玛县| 盐边县| 九龙县| 台中市| 昔阳县| 张掖市| 崇仁县| 宿松县| 兰州市| 平阳县| 泸定县| 汉源县| 东乌珠穆沁旗| 茶陵县| 区。| 武宁县| 怀来县| 澄城县| 丽水市| 西峡县| 友谊县| 赞皇县| 蓝山县| 仪征市| 灵宝市| 通化市| 南涧| 霸州市| 七台河市| 察雅县|