zoukankan      html  css  js  c++  java
  • C# 设置文件夹的访问权限

    /// <summary>
            /// 设置文件夹的ACL
            /// </summary>
            /// <param name="folderPath">文件夹路径</param>
            /// <param name="userName">用户</param>
            /// <param name="rights">权限</param>
            /// <param name="allowOrDeny">拒绝访问</param>
            /// <returns></returns>
            public static bool SetFolderACL(
                string folderPath, 
                string userName, 
                FileSystemRights rights, 
                AccessControlType allowOrDeny)
            {
                //设定文件ACL继承
                InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    
                return SetFolderACL(folderPath, userName, rights, allowOrDeny, inherits, PropagationFlags.None, AccessControlModification.Add);
    
            }
    
            /// <summary>
            /// 设置文件夹的ACL
            /// </summary>
            /// <param name="folderPath">文件夹路径</param>
            /// <param name="userName">用户</param>
            /// <param name="rights">权限</param>
            /// <param name="allowOrDeny">拒绝访问</param>
            /// <param name="inherits">ACL继承</param>
            /// <param name="propagateToChildren">应用于子文件夹及子文件</param>
            /// <param name="addResetOrRemove">修改类型</param>
            /// <returns></returns>
            public static bool SetFolderACL(
                string folderPath, 
                string userName, 
                FileSystemRights rights, 
                AccessControlType allowOrDeny, 
                InheritanceFlags inherits, 
                PropagationFlags propagateToChildren, 
                AccessControlModification addResetOrRemove)
            {
                bool result;
                //获取目录信息
                var folder = new DirectoryInfo(folderPath);
                //获取当前文件夹ACL
                var dSecurity = folder.GetAccessControl(AccessControlSections.All);
                //设置新的ACL规则
                var accRule = new FileSystemAccessRule(userName, rights, inherits, propagateToChildren, allowOrDeny); 
                //修改文件夹ACL
                dSecurity.ModifyAccessRule(addResetOrRemove, accRule, out result);
                //应用新的ACL规则到文件夹
                folder.SetAccessControl(dSecurity);
                //返回结果
                return result;
    
            }

    下面这样调用

            var folderPath = System.AppDomain.CurrentDomain.BaseDirectory;
            var userName = "Users";
            SetFolderACL(folderPath, userName, FileSystemRights.FullControl, AccessControlType.Allow);
  • 相关阅读:
    解决SQL Server Compact 无法在64位系统下正常运行
    Mvc5+Entity Framework6 之二----在MVC中用Entity Framework实现基本的CRUD
    Asp.net MVC5中Html.DropDownList的使用
    C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
    MVC学习 (二) Razor语法
    MVC学习 (一)
    编程实现机器人相遇
    jquery优化引发的思考
    (续)检测到有潜在危险的 Request.Form 值
    检测到有潜在危险的 Request.Form 值
  • 原文地址:https://www.cnblogs.com/yayaxxww/p/4243828.html
Copyright © 2011-2022 走看看