C#操作Windows用户
首先需要引入System.DirectoryServices.dll
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Text; namespace ConsoleApplication1
{
class Program
{
private static readonly string PATH = "WinNT://" + Environment.MachineName; /// <summary>
/// 更改windows用户密码
/// </summary>
/// <param name="username">用户名</param>
/// <param name="newpassword">新密码</param>
public static void UpdateUserPassword(string username, string newpassword)
{
using (DirectoryEntry dir = new DirectoryEntry(PATH))
{
using (DirectoryEntry user = dir.Children.Find(username, "user"))
{
user.Invoke("SetPassword", new object[] { newpassword });
user.CommitChanges();
}
}
}
/// <summary>
/// 删除windows用户
/// </summary>
/// <param name="username">用户名</param>
public static void RemoveUser(string username)
{
using (DirectoryEntry dir = new DirectoryEntry(PATH))
{
using (DirectoryEntry user = dir.Children.Find(username, "User"))
{
dir.Children.Remove(user);
}
}
}
/// <summary>
/// 添加windows用户组
/// </summary>
/// <param name="groupName">组名称</param>
/// <param name="description">描述</param>
public static void AddGroup(string groupName, string description)
{
using (DirectoryEntry dir = new DirectoryEntry(PATH))
{
using (DirectoryEntry group = dir.Children.Add(groupName, "group"))
{
group.Invoke("Put", new object[] { "Description", description });
group.CommitChanges();
}
}
}
/// <summary>
/// 删除windows用户组
/// </summary>
/// <param name="groupName">组名称</param>
public static void RemoveGroup(string groupName)
{
using (DirectoryEntry dir = new DirectoryEntry(PATH))
{
using (DirectoryEntry group = dir.Children.Find(groupName, "Group"))
{
dir.Children.Remove(group);
}
}
} /// <summary>
/// 添加windows用户
/// </summary>
/// <param name="username">用户名</param>
/// <param name="password">密码</param>
/// <param name="group">所属组</param>
/// <param name="description">描述</param>
public static void AddUser(string username, string password, string group, string description)
{
using (DirectoryEntry dir = new DirectoryEntry(PATH))
{
using (DirectoryEntry user = dir.Children.Add(username, "User")) //增加用户名
{
user.Properties["FullName"].Add(username); //用户全称
user.Invoke("SetPassword", password); //用户密码
user.Invoke("Put", "Description", description);//用户详细描述
//user.Invoke("Put","PasswordExpired",1); //用户下次登录需更改密码
user.Invoke("Put", "UserFlags", ); //密码永不过期
//user.Invoke("Put", "UserFlags", 0x0040);//用户不能更改密码s
user.CommitChanges();//保存用户
using (DirectoryEntry grp = dir.Children.Find(group, "group"))
{
if (grp.Name != "")
{
grp.Invoke("Add", user.Path.ToString());//将用户添加到某组
}
}
}
}
} static void Main(string[] args)
{
AddUser("ggg", "aaaa", "Administrators", "henley");
}
}
}
C#操作Windows用户的更多相关文章
- 在Asp.net MVC中使用Authorization Manager (AzMan)进行Windows用户身份认证
背景 创建需要通过Windows用户进行身份认证的Asp.net MVC应用 要点 在Asp.net MVC应用基于Windows用户进行身份认证的方法有很多,如MVC自带的Windows认证就经常被 ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
- [No0000197]Windows用户都应该知道的运行命令
通过"运行"命令,运行Windows丰富工具的方法.如果您知道工具或任务的相应"运行"命令,那么您就知道访问所述工具或任务的最快方法. 以下是我们最喜欢的Run ...
- 全键盘操作Windows
计算机机用户在使用计算机的时候,是用键盘多一点?还是用鼠标多一点?如果是专业打字员,应该会说他使用键盘多一点,除此之外,多数人都会告诉你,他已经离不开鼠标了,没有鼠标,就不会操作电脑. 如果某一天 ...
- C#使用DirectoryEntry类操作Windows帐户
1.创建windows帐户 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /// <summary> /// 创建Windows帐户 /// </summa ...
- 浅谈.NET(C#)与Windows用户账户信息的获取
原文:浅谈.NET(C#)与Windows用户账户信息的获取 目录 1. 用户账户名称 - 使用Environment类 2. 用户账户信息 - 使用WindowsIdentity和IdentityR ...
- C# 操作本地用户和组(基本全功能)
今天学习了下怎么用.Net操作本地用户和组,因为目前网上还没看到一篇比较完整的文章,所以整理了下也分享出来,最后附带参考文档,方便深究的童鞋继续学习.========== 原创作品 作者:Yo ...
- 命令行添加用户的“作为服务登录”权利(添加Windows用户的时候,门道不是一般的多)good
1.打开控制台(“开始”|“运行”中输入:MMC) 2.“文件”菜单|“添加删除管理单元”|“添加...”|选“安全模板”|“关闭”. 3.在“C:\Windows\Security\template ...
- Smack[3]用户列表,头像,组操作,用户操作
用户列表 Smack主要使用Roster进行列表管理的 connection.getRoster(); /** * 返回所有组信息 <RosterGroup> * * @return Li ...
随机推荐
- BluetoothLE-Multi-Library
github地址:https://github.com/qindachang/BluetoothLE-Multi-Library BluetoothLE-Multi-Library 一个能够连接多台蓝 ...
- 简单封装微信小程序
一.不同环境配置封装 新建config文件夹,根据自己有不同环境设置不同的js文件 具体js文件内容: exports.config = { requestHost: 'https://******. ...
- SQL server 备份/恢复/压缩 进度查询
第一步,用 sp_who2 查出备份的sid(或在窗口中的连接属性中看) exec sp_who2 第二步,用以下查询获得运行情况(看 percent_complete列) SELECT sessio ...
- php封装数据库mysql, mysqli
<?php header("content-type:text/html;charset=utf-8"); class db{ //私有的静态属性 private ...
- ORA-12547: TNS:lost contact
碰到这个ORA-12547: TNS:lost contact的问题,翻了很多资料和METALINK,总结了一下原因: 1 是由于rpm包没有安装,对于我们的生产环境,此包是安装的. admin@p1 ...
- vs2012安装程序,无法注册ActiveX
最近开发环境换成了vs2012,用C#写了一个ActiveX插件程序,然后添加一个安装程序,但是安装后,ie无法识别AcitveX,在ie的Manage add-ons中也找不到,这在vs2010是没 ...
- A-Z,a-z,0-9的unicode编码表
1.转自:https://blog.csdn.net/fedawn/article/details/7307993 A-Z 的 Unicode 字符编码表 十进制 十六进制 1.“A”的 U ...
- springmvc源码分析----入门看springmvc的加载过程
接上一篇我们写的入门---http://www.cnblogs.com/duanxiaojun/p/6591448.html 今天从这个门里进去我们看springmvc是如何在容器启动的时候将各个模块 ...
- 4 pyspark学习---RDD
开始新的东西,其实很多操作在第二篇的时候就有所介绍啦.在这里继续学习一遍加深一下印象. 1关于RDD (1) RDD-----Resilient Distributed Dataset,弹性分布式数据 ...
- awk用法总结
简介 awk的命名来自于他的三位创始人Alfred Aho .Peter Weinberger 和 Brian Kernighan 的姓氏的首字母. 有多种版本:New awk(nawk),GNU a ...