c# 對(duì)windows用戶和組操作實(shí)例
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); //密碼永不過(guò)期
//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);
}
}
}
}
}
相關(guān)文章
使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡(jiǎn)歷并且自動(dòng)發(fā)送邀請(qǐng)短信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2019-03-03在.net應(yīng)用程序中運(yùn)行其它EXE文件的方法
這篇文章主要介紹了在.net應(yīng)用程序中運(yùn)行其它EXE文件的方法,涉及C#進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05C#中IsNullOrEmpty和IsNullOrWhiteSpace的使用方法及區(qū)別解析
今天我們將探討C#中兩個(gè)常用的字符串處理方法:IsNullOrEmpty和IsNullOrWhiteSpace,本文中,我們將詳細(xì)解釋這兩個(gè)方法的功能和使用場(chǎng)景,并幫助您更好地理解它們之間的區(qū)別,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-07-07C#使用MailAddress類發(fā)送html格式郵件的實(shí)例代碼
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧2013-11-11c#異步讀取數(shù)據(jù)庫(kù)與異步更新ui的代碼實(shí)現(xiàn)
這篇文章主要介紹了c#從數(shù)據(jù)庫(kù)里取得數(shù)據(jù)并異步更新ui的方法,大家參考使用吧2013-12-12