這幾天使用C#操作windows帳戶相當糾結,以前沒做過,google翻閱了不少資料,嘗試不少方法,終於解決了我的問題。
1.創建windows帳戶
01
/// <summary>
02
/// 創建Windows帳戶
03
/// </summary>
04
/// <param name="pathname"></param>
05
/// <returns></returns>
06
public
static
void
CreateLocalUser(
string
username,
string
password,
string
description)
07
{
08
DirectoryEntry localMachine =
new
DirectoryEntry(
"WinNT://"
+ Environment.MachineName +
",computer"
);
09
var newUser = localMachine.Children.Add(username,
"user"
);
10
newUser.Invoke(
"SetPassword"
,
new
object
[] { password });
11
newUser.Invoke(
"Put"
,
new
object
[] {
"Description"
, description });
12
newUser.CommitChanges();
13
localMachine.Close();
14
newUser.Close();
15
}
2.更改Windows帳戶密碼
01
/// <summary>
02
/// 更改Windows帳戶密碼
03
/// </summary>
04
/// <param name="username"></param>
05
/// <param name="oldPwd"></param>
06
/// <param name="newPwd"></param>
07
public
static
void
ChangeWinUserPasswd(
string
username,
string
oldPwd,
string
newPwd)
08
{
09
DirectoryEntry localMachine =