其它方法見.用C#在本地建立一個Windows帳戶(DirectoryServices)
http://yaosansi.com/blog/article.asp?id=917
在WinForm下該程式可寫成: (已測試通過,需要以Administrator身份運行)
using System;
namespace eg
{
class test
{
static void Main()
{
//聲明一個程式資訊類
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
//設定外部程式名
Info.FileName = "net.exe";
//設定外部程式的啟動參數(命令列參數)為test.txt
Info.Arguments = " user abc /add";
//設定外部程式工作目錄為 D:\
Info.WorkingDirectory = "D:\\";
//聲明一個程式類
System.Diagnostics.Process Proc ;
try
{
//
//啟動外部程式
//
Proc = System.Diagnostics.Process.Start(Info);
}
catch(System.ComponentModel.Win32Exception e)
{
Console.WriteLine("系統找不到指定的程式檔案。\r{0}", e);
return;
}
}
}
}
注:ASPNET帳戶隸屬於Users組,Users組對電腦沒有完全控制權,涉及到系統管理的一些命令就無法使用了;至於為什麼在進程中看不到是因為該條語句是以命令列啟動並執行,要看效果的話只能在CMD下: net user 查看
添加管理員:
System.Diagnostics.Process.Start("CMD.exe","/c net user admin 123456 /add");
System.Diagnostics.Process.Start("CMD.exe","/c net localgroup administrators admin /add");
修改admin的密碼為250:
System.Diagnostics.Process.Start("CMD.exe","/c net user admin 250 ");
刪除管理員:
System.Diagnostics.Process.Start("CMD.exe","/c net user admin /del");
在CMD下就可以完全操作了,可以直接調用命令列工具如 System.Diagnostics.Process.Start("net user ...");
net user 用法
用於增加/建立/改動使用者帳戶
文法:
net user <username> [password or *] [/add] [options] [/domain]
net user <username] /delete /domain
username 帳號名
password 分配或改變密碼
* 密碼提示
/domain 在一個域中執行
/add 建立一個帳號
/delete 刪除一個帳戶
/active:[yes or no] 啟用或停止一個帳號
/comment:"<text>" 加入描述性說明
/counterycode.nnn nnn 是系統編碼. 0為系統預設值
/expires:<date or never> 帳號到期時間.格式:月,日,年或日,月,年(格式由國家代碼決定)
/fullname:"<name>" 帳號全名
/homedir:<path> 使用者主目錄路徑
/passwordchg:[yes or no] 設定使用者能否更改密碼
/passwordreq:[yes or no] 設定使用者是否要求輸入密碼
/profilepath:<path> 設定環境檔案路徑
/scriptpath:<path> 登入指令碼路徑
/times:<times or all> 使用者可以登入的小時數
/usercomment:"<text>" 帳號說明資訊
/workstations:<machine names> 允許登入的使用者名. * 表示所有使用者