C# 控制NTFS目录权限

using System;
using System.Collections.Generic;
using System.Security.AccessControl;
using System.IO;
namespace SovSafe
{
    class NTFS_Control
    {
        public NTFS_Control()
        { }

        // 添加 指定目录 指定用户 指定的 权限
        public void AddDirectorySecurity(string FileName, string Account, string UserRights)
        {
            FileSystemRights Rights = new FileSystemRights();

            if (UserRights.IndexOf("R") >= 0)
            {
                Rights = Rights | FileSystemRights.Read;
            }
            if (UserRights.IndexOf("C") >= 0)
            {
                Rights = Rights | FileSystemRights.ChangePermissions;
            }
            if (UserRights.IndexOf("F") >= 0)
            {
                Rights = Rights | FileSystemRights.FullControl;
            }
            if (UserRights.IndexOf("W") >= 0)
            {
                Rights = Rights | FileSystemRights.Write;
            }

            bool ok;
            DirectoryInfo dInfo = new DirectoryInfo(FileName);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            InheritanceFlags iFlags = new InheritanceFlags();
            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, Rights, iFlags, PropagationFlags.None, AccessControlType.Allow);
            dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, out ok);
            dInfo.SetAccessControl(dSecurity);

        }

        // 获取 指定目录 除Administrators和SYSTEM之外的 权限列表
        public List<string> GetDirectoryAccountSecurity(string DirName)
        {
            List<string> dAccount = new List<string>();
            DirectoryInfo dInfo = new DirectoryInfo(DirName);
            if (dInfo.Exists)
            {
                DirectorySecurity sec = Directory.GetAccessControl(DirName, AccessControlSections.All);
                foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                {
                    if (rule.IdentityReference.Value != @"NT AUTHORITY/SYSTEM" && rule.IdentityReference.Value != @"BUILTIN/Administrators")
                        dAccount.Add(rule.IdentityReference.Value);
                }
            }
            return dAccount;
        }

        // 移除 指定目录 指定用户的 权限
        public void RemoveDirectoryAccountSecurity(string DirName, string Account)
        {
            DirectoryInfo dInfo = new DirectoryInfo(DirName);
            if (dInfo.Exists)
            {
                System.Security.Principal.NTAccount myAccount = new System.Security.Principal.NTAccount(System.Environment.MachineName, Account);

                DirectorySecurity dSecurity = dInfo.GetAccessControl();

                FileSystemAccessRule AccessRule = new FileSystemAccessRule(Account, FileSystemRights.FullControl, AccessControlType.Allow);
                FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, FileSystemRights.FullControl, AccessControlType.Deny);

                InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
                PropagationFlags pFlags = PropagationFlags.InheritOnly | PropagationFlags.NoPropagateInherit;

                dSecurity.AccessRuleFactory(myAccount, 983551, false, iFlags, pFlags, AccessControlType.Allow);

                dSecurity.RemoveAccessRuleAll(AccessRule);
                dSecurity.RemoveAccessRuleAll(AccessRule2);

                dInfo.SetAccessControl(dSecurity);
            }
        }

        // 获取 指定文件 除Administrators和SYSTEM之外的 权限列表
        public List<string> GetFileAccountSecurity(string fileName)
        {
            List<string> fAccount = new List<string>();
            FileInfo fInfo = new FileInfo(fileName);
            if (fInfo.Exists)
            {
                FileSecurity fec = File.GetAccessControl(fileName, AccessControlSections.All);
                foreach (FileSystemAccessRule rule in fec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
                {
                    if (rule.IdentityReference.Value != @"NT AUTHORITY/SYSTEM" && rule.IdentityReference.Value != @"BUILTIN/Administrators")
                        fAccount.Add(rule.IdentityReference.Value);
                }
            }
            return fAccount;
        }

        // 移除 指定文件 指定用户的 权限
        public void RemoveFileAccountSecurity(string fileName, string Account)
        {

            FileInfo fInfo = new FileInfo(fileName);
            if (fInfo.Exists)
            {
                FileSecurity fSecurity = fInfo.GetAccessControl();
                FileSystemAccessRule AccessRule = new FileSystemAccessRule(Account, FileSystemRights.FullControl, AccessControlType.Allow);
                FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, FileSystemRights.FullControl, AccessControlType.Deny);
                fSecurity.RemoveAccessRuleAll(AccessRule);
                fSecurity.RemoveAccessRuleAll(AccessRule2);
                fInfo.SetAccessControl(fSecurity);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值