现在的位置: 首页 > 综合 > 正文

c#添加、删除、修改windows用户,目录用户

2013年09月16日 ⁄ 综合 ⁄ 共 4491字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.DirectoryServices;
using System.Security.AccessControl;

namespace ConsoleApplication1
{
    public class Test
    {

        public static void start()
        {

            Test.Add("江湖小子", "123456",".net加的用户");
            //Test.AddFileSecurity("江湖小子", "D://temp//mapkey");
        }

        /// <summary>
        /// 添加windows用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="description">描述</param>
        /// <returns></returns>
        public static bool Add(string username, string password, string description)
        {
            try
            {
                DirectoryEntry dir = new DirectoryEntry("WinNT://{0},computer".Formats(Environment.MachineName));
                DirectoryEntry newuser = dir.Children.Add(username, "user");
                newuser.Invoke("SetPassword", new object[] { password });
                newuser.Invoke("Put", new object[] { "Description", description });
                //newuser.Invoke("Put","PasswordExpired",1); //用户下次登录需更改密码
                newuser.Invoke("Put", "UserFlags", 0x0040);//用户不能更改密码
                newuser.CommitChanges();
                dir.Close();
                newuser.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 更改windows用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="description">描述</param>
        /// <returns></returns>
        public static bool Update(string username, string password, string description)
        {
            try
            {
                DirectoryEntry dir = new DirectoryEntry("WinNT://{0},computer".Formats(Environment.MachineName));
                DirectoryEntry newuser = dir.Children.Find(username, "user");
                newuser.Invoke("SetPassword", new object[] { password });
                newuser.Invoke("Put", new object[] { "Description", description });
                newuser.CommitChanges();
                dir.Close();
                newuser.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 删除windows用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public static bool Remove(string username)
        {
            DirectoryEntry user;
            DirectoryEntry dir = null;
            try
            {
                dir = new DirectoryEntry("WinNT://{0},computer".Formats(Environment.MachineName));
                user = dir.Children.Find(username, "User");
                dir.Children.Remove(user);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                dir.Dispose();

            }
        }

        /// <summary>
        /// 添加目录账户
        /// </summary>
        /// <param name="account">账户</param>
        /// <param name="path">路径(可以是目录或文件)</param>
        public static void AddFileSecurity(string account, string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            DirectorySecurity ds = dir.GetAccessControl();
            FileSystemAccessRule ar1 = new FileSystemAccessRule(account, FileSystemRights.FullControl, AccessControlType.Allow);
            FileSystemAccessRule ar2 = new FileSystemAccessRule(account, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
            ds.AddAccessRule(ar1);
            ds.AddAccessRule(ar2);
            dir.SetAccessControl(ds);
        }

        /// <summary>
        /// 删除目录账户
        /// </summary>
        /// <param name="account">账户</param>
        /// <param name="path">路径</param>
        public static void RemoveFileSecurity(string account, string path)
        {
            DirectoryInfo dirinfo = new DirectoryInfo(path);
            if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
            {
                dirinfo.Attributes = FileAttributes.Normal;
            }
            //取得访问控制列表
            DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
            dirsecurity.RemoveAccessRuleAll(new FileSystemAccessRule(account, FileSystemRights.FullControl, AccessControlType.Allow));
            dirinfo.SetAccessControl(dirsecurity);
        }

    }

    public static class StringExtends
    {
        public static string Formats(this string input, params object[] args)
        {
            return String.Format(input, args);
        }

抱歉!评论已关闭.