SoFunction
Updated on 2025-03-06

C# operation examples for windows users and groups


using System;
using ;
using ;
using ;
using ;
namespace
{
    ///

/// Computer user and group operation class
    ///

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

/// Add Windows user
        ///

/// username
/// password
/// Group
/// describe
        public static void AddUser(string username, string password, string group, string description)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
using (DirectoryEntry user = (username, "User")) //Add username
                {
["FullName"].Add(username); //The full name of the user
("SetPassword", password); //User Password
("Put", "Description", description);// User detailed description
//("Put","PasswordExpired",1); //The user needs to change the password next time he logs in
("Put", "UserFlags", 66049); //Password never expires
//("Put", "UserFlags", 0x0040);//User cannot change passwords
();//Save user
                    using (DirectoryEntry grp = (group, "group"))
                    {
                        if ( != "")
                        {
("Add", ());//Add the user to a group
                        }
                    }
                }
            }
        }
        ///

/// Change the Windows user password
        ///

/// username
/// New Password
        public static void UpdateUserPassword(string username, string newpassword)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry user = (username, "user"))
                {
                    ("SetPassword", new object[] { newpassword });
                    ();
                }
            }
        }
        ///

/// Delete windows user
        ///

/// username
        public static void RemoveUser(string username)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry user = (username, "User"))
                {
                    (user);
                }
            }
        }
        ///

/// Add windows user group
        ///

/// Group name
/// describe
        public static void AddGroup(string groupName, string description)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry group = (groupName, "group"))
                {
                    ("Put", new object[] { "Description", description });
                    ();
                }
            }
        }
        ///

/// Delete the windows user group
        ///

/// Group name
        public static void RemoveGroup(string groupName)
        {
            using (DirectoryEntry dir = new DirectoryEntry(PATH))
            {
                using (DirectoryEntry group = (groupName, "Group"))
                {
                    (group);
                }
            }
        }
    }
}