c# 對windows用戶和組操作實例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace JH.Framework.Security
{
///
/// 計算機用戶和組操作類
///
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);//用戶詳細描述
//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);
}
}
}
}
}
相關(guān)文章
使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的方法
這篇文章主要給大家介紹了關(guān)于如何使用C# CefSharp Python采集某網(wǎng)站簡歷并且自動發(fā)送邀請短信的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧2019-03-03C#中IsNullOrEmpty和IsNullOrWhiteSpace的使用方法及區(qū)別解析
今天我們將探討C#中兩個常用的字符串處理方法:IsNullOrEmpty和IsNullOrWhiteSpace,本文中,我們將詳細解釋這兩個方法的功能和使用場景,并幫助您更好地理解它們之間的區(qū)別,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2023-07-07C#使用MailAddress類發(fā)送html格式郵件的實例代碼
這篇文章主要介紹如何使用C#的MailAddress類發(fā)送郵件的方法,大家參考使用吧2013-11-11c#異步讀取數(shù)據(jù)庫與異步更新ui的代碼實現(xiàn)
這篇文章主要介紹了c#從數(shù)據(jù)庫里取得數(shù)據(jù)并異步更新ui的方法,大家參考使用吧2013-12-12