[C#]创建Windows用户及组

简介: 原文:[C#]创建Windows用户及组 using System; using System.Collections.Generic; using System.Linq; using System.
原文: [C#]创建Windows用户及组

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.DirectoryServices.AccountManagement;
using System.Collections;

namespace UserTrans
{
    public static class Commons
    {
        static Configuration config;
        static AppSettingsSection appSetting;
        public static ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        static bool CreateLocalWindowsAccount(string userName, string passWord, string displayName, string description, string groupName, bool canChangePwd, bool pwdExpires)
        {
            bool retIsSuccess = false;
            try
            {
                PrincipalContext context = new PrincipalContext(ContextType.Machine);
                UserPrincipal user = new UserPrincipal(context);
                user.SetPassword(passWord);
                user.DisplayName = displayName;
                user.Name = userName;
                user.Description = description;
                user.UserCannotChangePassword = canChangePwd;
                user.PasswordNeverExpires = pwdExpires;
                user.Save();

                GroupPrincipal group = GroupPrincipal.FindByIdentity(context, groupName);
                group.Members.Add(user);
                group.Save();
                retIsSuccess = true;
            }
            catch (Exception ex)
            {
                retIsSuccess = false;
            }
            return retIsSuccess;
        }

        static GroupPrincipal CreateGroup(string groupName, Boolean isSecurityGroup)
        {
            GroupPrincipal retGroup = null;
            try
            {
                retGroup = IsGroupExist(groupName);
                if (retGroup == null)
                {
                    PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
                    GroupPrincipal insGroupPrincipal = new GroupPrincipal(ctx);
                    insGroupPrincipal.Name = groupName;
                    insGroupPrincipal.IsSecurityGroup = isSecurityGroup;
                    insGroupPrincipal.GroupScope = GroupScope.Local;
                    insGroupPrincipal.Save();
                    retGroup = insGroupPrincipal;
                }
            }
            catch (Exception ex)
            {

            }

            return retGroup;
        }

        static GroupPrincipal IsGroupExist(string groupName)
        {
            GroupPrincipal retGroup = null;
            try
            {
                PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
                GroupPrincipal qbeGroup = new GroupPrincipal(ctx);
                PrincipalSearcher srch = new PrincipalSearcher(qbeGroup);
                foreach (GroupPrincipal ingrp in srch.FindAll())
                {
                    if (ingrp != null && ingrp.Name.Equals(groupName))
                    {
                        retGroup = ingrp;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return retGroup;
        }

        public static int UpdateGroupUsers(string groupName, List<string> usersName)
        {
            List<string> addedUsers = new List<string>();
            int retAddCount = 0;

            GroupPrincipal qbeGroup = CreateGroup(groupName, false);
            foreach (UserPrincipal user in qbeGroup.GetMembers())
            {
                if (usersName.Contains(user.Name))
                {
                    addedUsers.Add(user.Name);
                    retAddCount++;
                }
                else
                {
                    user.Delete();
                }
            }
            foreach (string addedUserName in addedUsers)
            {
                usersName.Remove(addedUserName);
            }
            foreach (string addUserName in usersName)
            {
                bool isSuccess = CreateLocalWindowsAccount(addUserName, "password", addUserName, "", groupName, false, false);
                if (isSuccess)     retAddCount++;
            }return retAddCount;
        }

} }

 

  参考

http://msdn.microsoft.com/zh-cn/library/bb924542(v=vs.90).aspx

目录
相关文章
|
9月前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
122 5
|
9月前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
|
人工智能 搜索推荐 C#
C#开源且免费的Windows桌面快速预览神器 - QuickLook
C#开源且免费的Windows桌面快速预览神器 - QuickLook
225 0
|
4月前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
131 0
|
4月前
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
83 0
|
4月前
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
125 0
|
6月前
|
C# Windows
C# 创建 Windows Service 项目
C# 创建 Windows Service 项目
53 1
|
5月前
|
关系型数据库 数据库 PostgreSQL
在C#中获取与设置Windows的字符编码方式
通过以上步骤,你可以在Docker环境下有效地重启PostgreSQL服务。这对于维护数据库健康、应用更新或环境配置更改后确保数据库服务正常运行至关重要。根据你的具体需求和环境设置,选择合适的方法来执行重启操作。
33 0
|
安全 C# Windows
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
138 0
|
9月前
|
API C# C++
微软官方发布的C#开源、免费、实用的Windows工具箱
微软官方发布的C#开源、免费、实用的Windows工具箱
127 0