博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# Windows帐户和目录添加用户权限方法
阅读量:5092 次
发布时间:2019-06-13

本文共 4532 字,大约阅读时间需要 15 分钟。

///     /// 目录权限    ///     public enum FloderRights    {        FullControl,        Read,        Write    }        ///         /// 创建Windows帐户        ///         ///         /// 
public static void CreateLocalUser(string username, string password, string description) { DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); var NewUser = dirEntry.Children.Add(username, "user"); NewUser.Invoke("SetPassword", new object[] { password }); NewUser.Invoke("Put", new object[] { "Description", description }); NewUser.CommitChanges(); } /// /// 更改Windows帐户密码 /// /// /// /// public static void ChangeWinUserPasswd(string username, string oldPwd, string newPwd) { DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); DirectoryEntry userEntry = dirEntry.Children.Find(username, "user"); object[] password = new object[] { newPwd, oldPwd }; object ret = userEntry.Invoke("ChangePassword", password); userEntry.CommitChanges(); } /// /// 给目录添加用户和权限 /// /// /// /// public static void AddPathRights(string pathname, string username, FloderRights qx) { DirectoryInfo dirinfo = new DirectoryInfo(pathname); if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0) { dirinfo.Attributes = FileAttributes.Normal; } //取得访问控制列表 DirectorySecurity dirsecurity = dirinfo.GetAccessControl(); // string strDomain = Dns.GetHostName(); switch (qx) { case FloderRights.FullControl: dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow)); break; case FloderRights.Read: dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow)); break; case FloderRights.Write: dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow)); break; default: dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny)); break; } dirinfo.SetAccessControl(dirsecurity); //取消目录从父继承 DirectorySecurity dirSecurity = System.IO.Directory.GetAccessControl(pathname); dirSecurity.SetAccessRuleProtection(true, false); System.IO.Directory.SetAccessControl(pathname, dirSecurity); //AccessControlType.Allow允许访问受保护对象//Deny拒绝访问受保护对象 //FullControl、Read 和 Write 完全控制,读,写 //FileSystemRights.Write写入//Delete删除 //DeleteSubdirectoriesAndFiles删除文件夹和文件//ListDirectory读取 //Modify读写删除-修改//只读打开文件和复制// } /// /// 判断Windows用户是否存在 /// /// ///
public static bool ExistWinUser(string username) { try { using (DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer")) { //删除存在用户 var delUser = dirEntry.Children.Find(username, "user"); return delUser != null; } } catch { return false; } } /// /// 删除Windows用户 /// /// ///
public static bool DeleteWinUser(string username) { try { using (DirectoryEntry dirEntry = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer")) { //删除存在用户 var delUser = dirEntry.Children.Find(username, "user"); if (delUser != null) { dirEntry.Children.Remove(delUser); } } return true; } catch { return false; } }
posted on
2010-07-22 09:29  阅读(
...) 评论(
...) 收藏

转载于:https://www.cnblogs.com/skynothing/archive/2010/07/22/1782720.html

你可能感兴趣的文章
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
IOS--沙盒机制
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>
【题解】[P4178 Tree]
查看>>
Mongo自动备份
查看>>
cer证书签名验证
查看>>
synchronized
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>
QML学习笔记之一
查看>>
App右上角数字
查看>>
从.NET中委托写法的演变谈开去(上):委托与匿名方法
查看>>
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>
IdentityServer4-用EF配置Client(一)
查看>>
WPF中实现多选ComboBox控件
查看>>