NET C#创建WINDOWS系统用户

时间:2021-10-26 02:09:46

原文:NET C#创建WINDOWS系统用户

 

/前提是当前用户有相应的权限

/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 ); //密码永不过期
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 obComputer = new DirectoryEntry("WinNt://" + Environment.MachineName);
DirectoryEntry obUser = obComputer.Children.Find(Username,"User");
obUser.Invoke("SetPassword", Userpassword);
obUser.CommitChanges();
obUser.Close();
obComputer.Close();
return true;
}
catch
{
return false;
}
}
}
}

 

http://topic.csdn.net/u/20100902/08/2ecbcb74-158f-4530-b3c6-5a805a4f81b2.html