using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//using System.DirectoryServices;
using System.Diagnostics;
using System.IO;
namespace CreateUser
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.txtPath.Text =Application.StartupPath+@"\User.txt";
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/* private void AddUser( string strLogin, string strPwd)
{ strDoamin=".";
DirectoryEntry obDirEntry = null;
obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
obUser.Properties["FullName"].Add("sifang");
obUser.Properties["Description"].Add("sifang");
obUser.Properties["Usercannotchangepassword"].Add(true);
obUser.Properties["Passwordneverexpires"].Add(true);
object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
*/
private void button2_Click(object sender, System.EventArgs e)
{
readfile();
}
public void CreateUser(string stru,string strp)
{
Process msgProcess = new Process();
msgProcess.StartInfo.FileName = @"net.exe";
msgProcess.StartInfo.CreateNoWindow = true;
string cmd1="user "+stru+" "+strp+ " /add";
//string cmd2="user "+stru+" /delete";
msgProcess.StartInfo.Arguments = cmd1;
//msgProcess.StartInfo.Arguments = cmd2;
msgProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
msgProcess.StartInfo.UseShellExecute = false;
msgProcess.StartInfo.RedirectStandardOutput = true;
msgProcess.Start();
msgProcess.WaitForExit();
}
private void readfile()
{
if(!File.Exists(this.txtPath.Text))
{
MessageBox.Show("请输入文件路径或者文件路径不存在","错误信息");
}
else
{
StreamReader objReader = new StreamReader(this.txtPath.Text);
string sLine="";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
string[] strArr=sOutput.Split(',');
//for(int i=0;i<strArr.Length;i++)
//{
CreateUser(strArr[0],strArr[0]);
//}
}
MessageBox.Show("共创建"+arrText.Count.ToString()+"用户!");
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//using System.DirectoryServices;
using System.Diagnostics;
using System.IO;
namespace CreateUser
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox txtPath;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.txtPath.Text =Application.StartupPath+@"\User.txt";
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/* private void AddUser( string strLogin, string strPwd)
{ strDoamin=".";
DirectoryEntry obDirEntry = null;
obDirEntry = new DirectoryEntry("WinNT://" + strDoamin);
DirectoryEntries entries = obDirEntry.Children;
DirectoryEntry obUser = entries.Add(strLogin, "User");
obUser.Properties["FullName"].Add("sifang");
obUser.Properties["Description"].Add("sifang");
obUser.Properties["Usercannotchangepassword"].Add(true);
obUser.Properties["Passwordneverexpires"].Add(true);
object obRet = obUser.Invoke("SetPassword", strPwd);
obUser.CommitChanges();
}
*/
private void button2_Click(object sender, System.EventArgs e)
{
readfile();
}
public void CreateUser(string stru,string strp)
{
Process msgProcess = new Process();
msgProcess.StartInfo.FileName = @"net.exe";
msgProcess.StartInfo.CreateNoWindow = true;
string cmd1="user "+stru+" "+strp+ " /add";
//string cmd2="user "+stru+" /delete";
msgProcess.StartInfo.Arguments = cmd1;
//msgProcess.StartInfo.Arguments = cmd2;
msgProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
msgProcess.StartInfo.UseShellExecute = false;
msgProcess.StartInfo.RedirectStandardOutput = true;
msgProcess.Start();
msgProcess.WaitForExit();
}
private void readfile()
{
if(!File.Exists(this.txtPath.Text))
{
MessageBox.Show("请输入文件路径或者文件路径不存在","错误信息");
}
else
{
StreamReader objReader = new StreamReader(this.txtPath.Text);
string sLine="";
ArrayList arrText = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
arrText.Add(sLine);
}
objReader.Close();
foreach (string sOutput in arrText)
{
string[] strArr=sOutput.Split(',');
//for(int i=0;i<strArr.Length;i++)
//{
CreateUser(strArr[0],strArr[0]);
//}
}
MessageBox.Show("共创建"+arrText.Count.ToString()+"用户!");
}
}
}
}
一种ADSI using System.DirectoryServices;
另一种调用net.exe来构造命令来执行。
在使用ADSI时有些属性无法查到。使用Net.exe更灵活一些。
代码下载https://files.cnblogs.com/Hover/CreateUser.rar