//WinNT用戶管理
using System;
using System.DirectoryServices;
namespace Host.AdminManager.Inc
{
public class WindwosUser
{
//創建NT用戶
//傳入參數:Username要創建的用戶名,UserpassWord用戶密碼,Path主文件夾路徑
public static bool CreateNTUser(string Username,string UserpassWord,string Path)
{
DirectoryEntry obDirEntry = null;
try
{
obDirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry obUser = obDirEntry.Children.Add(Username, "User"); //增加用戶名
obUser.PropertIEs["FullName"].Add(Username); //
用戶全稱
obUser.Invoke("SetPassword", UserpassWord); //用戶密碼
obUser.Invoke("Put", "Description","Test User from .Net");//用戶詳細描述
//obUser.Invoke("Put","PassWordExpired",1); //用戶下次登錄需更改密碼
obUser.Invoke("Put","UserFlags",66049); //密碼永不過期
obUser.Invoke("Put","HomeDirectory",Path); //主文件夾路徑
obUser.CommitChanges();//保存用戶
DirectoryEntry grp = obDirEntry.Children.Find("Users", "group");//Users組
if(grp.Name!="")
{
grp.Invoke("Add",obUser.Path.ToString());//將用戶添加到某組
}
return true;
}
catch
{
return false;
}
}
//刪除NT用戶
//傳入參數:Username用戶名
public static bool DelNTUser(string Username)
{
try
{
DirectoryEntry obComputer = new DirectoryEntry("WinNt://" + Environment.MachineName);//獲得計算機實例
DirectoryEntry obUser = obComputer.Children.Find(Username,"User");//找得用戶
obComputer.Children.Remove(obUser);//刪除用戶
return true;
}
catch
{
return false;
}
}
//修改NT用戶密碼
//傳入參數:Username用戶名,UserpassWord用戶新密碼
public static bool InitNTPwd(string Username,string UserpassWord)
{
try
{
DirectoryEntry