C# 管理WinNt 帐号

//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 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;
}
}
}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值