|
Posted on
2010-08-27 10:50
linFen
阅读( 1357)
评论()
编辑
收藏
举报
005 |
public enum FloderRights |
015 |
/// <param name="pathname"></param> |
016 |
/// <returns></returns> |
017 |
public static void CreateLocalUser( string username, string password, string description) |
019 |
DirectoryEntry dirEntry = new DirectoryEntry( "WinNT://" + Environment.MachineName + ",computer" ); |
020 |
var NewUser = dirEntry.Children.Add(username, "user" ); |
021 |
NewUser.Invoke( "SetPassword" , new object [] { password }); |
022 |
NewUser.Invoke( "Put" , new object [] { "Description" , description }); |
023 |
NewUser.CommitChanges(); |
030 |
/// <param name="username"></param> |
031 |
/// <param name="oldPwd"></param> |
032 |
/// <param name="newPwd"></param> |
033 |
public static void ChangeWinUserPasswd( string username, string oldPwd, string newPwd) |
035 |
DirectoryEntry dirEntry = new DirectoryEntry( "WinNT://" + Environment.MachineName + ",computer" ); |
036 |
DirectoryEntry userEntry = dirEntry.Children.Find(username, "user" ); |
037 |
object [] password = new object [] { newPwd, oldPwd }; |
038 |
object ret = userEntry.Invoke( "ChangePassword" , password); |
039 |
userEntry.CommitChanges(); |
045 |
/// <param name="pathname"></param> |
046 |
/// <param name="username"></param> |
047 |
/// <param name="qx"></param> |
048 |
public static void AddPathRights( string pathname, string username, FloderRights qx) |
050 |
DirectoryInfo dirinfo = new DirectoryInfo(pathname); |
051 |
if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0) |
053 |
dirinfo.Attributes = FileAttributes.Normal; |
056 |
DirectorySecurity dirsecurity = dirinfo.GetAccessControl(); |
060 |
case FloderRights.FullControl: |
061 |
dirsecurity.AddAccessRule( new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow)); |
063 |
case FloderRights.Read: |
064 |
dirsecurity.AddAccessRule( new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow)); |
066 |
case FloderRights.Write: |
067 |
dirsecurity.AddAccessRule( new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow)); |
070 |
dirsecurity.AddAccessRule( new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Deny)); |
074 |
dirinfo.SetAccessControl(dirsecurity); |
077 |
DirectorySecurity dirSecurity = System.IO.Directory.GetAccessControl(pathname); |
078 |
dirSecurity.SetAccessRuleProtection( true , false ); |
079 |
System.IO.Directory.SetAccessControl(pathname, dirSecurity); |
090 |
/// <param name="username"></param> |
091 |
/// <returns></returns> |
092 |
public static bool ExistWinUser( string username) |
096 |
using (DirectoryEntry dirEntry = new DirectoryEntry( "WinNT://" + Environment.MachineName + ",computer" )) |
099 |
var delUser = dirEntry.Children.Find(username, "user" ); |
100 |
return delUser != null ; |
112 |
/// <param name="username"></param> |
113 |
/// <returns></returns> |
114 |
public static bool DeleteWinUser( string username) |
118 |
using (DirectoryEntry dirEntry = new DirectoryEntry( "WinNT://" + Environment.MachineName + ",computer" )) |
121 |
var delUser = dirEntry.Children.Find(username, "user" ); |
124 |
dirEntry.Children.Remove(delUser); |
|