使用c#创建windows本地用户帐号

来源:互联网 发布:卿烨科技 知乎 编辑:程序博客网 时间:2024/06/05 00:35

使用c#创建windows本地用户帐号

Using the Windows net command, it’s easy to create local Windows User Accounts. The syntax for the net command is:

net user [username] [password] /ADD
The following C# function takes in three parameters -- username, password and home directory.

using System.Diagnostics;

public void CreateLocalUser(string username, string password, string homedir)
{
if (!Directory.Exists(homedir))
Directory.CreateDirectory(homedir);

Process MyProc = new Process();
MyProc.StartInfo.WorkingDirectory = "C:/WINNT/SYSTEM32";
MyProc.StartInfo.FileName = "net.exe";
MyProc.StartInfo.UseShellExecute = false;
MyProc.StartInfo.RedirectStandardError = true;
MyProc.StartInfo.RedirectStandardInput = true;
MyProc.StartInfo.RedirectStandardOutput = true;
MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

MyProc.StartInfo.Arguments = @" user " + username + @" " + password + @" /ADD /ACTIVE:YES " +
@"/EXPIRES:NEVER /FULLNAME:" + username + @" /HOMEDIR:""" +
homedir + @""" /PASSWORDCHG:NO /PASSWORDREQ:YES";

MyProc.Start();
MyProc.WaitForExit();
MyProc.Close();
}

原创粉丝点击