zoukankan      html  css  js  c++  java
  • C#文件夹权限操作工具类

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.AccessControl;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CvNetVideo.Play
    {
        /// <summary>
        /// 目录权限辅助类
        /// </summary>
        public class DictionaryHelper
        {
            /// <summary>
            /// 需要足够的权限才能执行此操作:Get filepath rights
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static List<FileSystemRights> GetRights(string path)
            {
                List<FileSystemRights> ret = new List<FileSystemRights>();
    
                DirectorySecurity dirSec = Directory.GetAccessControl(path, AccessControlSections.All);
                AuthorizationRuleCollection rules = dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                foreach (FileSystemAccessRule rule in rules)
                {
                    ret.Add(rule.FileSystemRights);
                }
                return ret;
            }
    
            //public static void AccessExample() {
            //    try
            //    {
            //        string DirectoryName = "TestDirectory";
    
            //        Console.WriteLine("Adding access control entry for " + DirectoryName);
    
            //        // Add the access control entry to the directory.
            //        AddDirectorySecurity(DirectoryName, @"MYDOMAINMyAccount", FileSystemRights.ReadData, AccessControlType.Allow);
    
            //        Console.WriteLine("Removing access control entry from " + DirectoryName);
    
            //        // Remove the access control entry from the directory.
            //        RemoveDirectorySecurity(DirectoryName, @"MYDOMAINMyAccount", FileSystemRights.ReadData, AccessControlType.Allow);
    
            //        Console.WriteLine("Done.");
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine(e);
            //    }
    
            //    Console.ReadLine();
            //}
    
            // Adds an ACL entry on the specified directory for the specified account.
            public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);
    
                // Get a DirectorySecurity object that represents the 
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
    
                // Add the FileSystemAccessRule to the security settings. 
                dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
                                                                Rights,
                                                                ControlType));
    
                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);
    
            }
    
            // Removes an ACL entry on the specified directory for the specified account.
            public static void RemoveDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
            {
                // Create a new DirectoryInfo object.
                DirectoryInfo dInfo = new DirectoryInfo(FileName);
    
                // Get a DirectorySecurity object that represents the 
                // current security settings.
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
    
                // Add the FileSystemAccessRule to the security settings. 
                dSecurity.RemoveAccessRule(new FileSystemAccessRule(Account,
                                                                Rights,
                                                                ControlType));
    
                // Set the new access settings.
                dInfo.SetAccessControl(dSecurity);
    
            }
        }
    }
    
  • 相关阅读:
    python爬虫 js逆向之取巧秒解webpack打包的加密参数
    程序员半夜泡奶粉,睡不着了
    擴展Membership建立中小型WEB權限框架(一)
    Gridview小技巧保存選擇狀態
    sql server 2005行列轉換
    一个简单的WEB流程图组件[demo]
    web效率14條規則(轉)
    常用javascript 表達式驗證[綜合轉載]
    SOA、反射+緩存
    数据表的设计原则(轉載)
  • 原文地址:https://www.cnblogs.com/boonya/p/9013564.html
Copyright © 2011-2022 走看看