日期:2015-07-06  浏览次数:3351 次

这几天使用C#操作windows帐户相当纠结,以前没做过,google翻阅了不少资料,尝试不少方法,终于解决了我的问题。

1.创建windows帐户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <summary>
/// 创建Windows帐户
/// </summary>
/// <param name="pathname"></param>
/// <returns></returns>
public static void CreateLocalUser(string username, string password, string description)
{
    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    var newUser = localMachine.Children.Add(username, "user");
    newUser.Invoke("SetPassword", new object[] { password });
    newUser.Invoke("Put", new object[] { "Description", description });
    newUser.CommitChanges();
    localMachine.Close();
    newUser.Close();
}

2.更改Windows帐户密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/// <summary>
/// 更改Windows帐户密码
/// </summary>
/// <param name="username"></param>
/// <param name="oldPwd"></param>
/// <param name="newPwd"></param>
public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd)
{
    DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
    DirectoryEntry user = localMachine.Children.Find(username, "user");
    object[] password = new object[] { oldPwd, newPwd };
    object ret = user.Invoke("ChangePassword", password);
    user.CommitChanges();
    localMachine.Close();
    user.Close();
}

3.判断Windows用户是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/// <summary>
/// 判断Windows用户是否存在
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static bool ExistWinUser(string username)
{
    try
    {
        using (DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
        {
            var user = localMachine.Children.Find(username, "user");
            return user != null;
        }
    }
    catch
    {
        return false;
    }
}

4.删除Windows用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/// <summary>
/// 删除Windows用户
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
public static bool DeleteWinUser(string username)
{
    try
    {
        using (DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
        {
            //删除存在用户
            var delUser = localMachine.Children.Find(username, "user");
            if (delUser != null)
            {
                localMachine.Children.Remove(delUser);
            }
        }
        return true;
    }
    catch
    {
        return false;
    }
}

 

5.启用/禁用windows帐户

1
2
3
4
5
6
7
8
9
10
11
12
/// <summary>
/// 启用/禁用windows帐户
/// </summary>
/// <param name="username"></param>
public static void Disable(string username, bool isDisable)
{
    var userDn = "WinNT://" + Environment.MachineName + "/" + username + ",user";
    DirectoryEntry user = new DirectoryEntry(userDn);
    user.InvokeSet("AccountDisabled", isDisable);
    user.CommitChanges();
    user.Close();
}