zoukankan      html  css  js  c++  java
  • c# 代码控制文件夹权限时,只显示特殊权限的问题

    问题描述:

    使用如下代码:

    DirectorySecurity fSec = new DirectorySecurity();
    fSec.AddAccessRule(new FileSystemAccessRule(@"Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
    System.IO.Directory.SetAccessControl(FolderPath, fSec);

    控制文件夹权限,本应该为完全控制,但结果只显示特殊权限。

    文件夹的特殊权限表示,子文件和文件夹的权限不一致导致。因此代码做如下修改

    /// <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;

    }

    文章出处

    https://www.cnblogs.com/yayaxxww/p/4243828.html

  • 相关阅读:
    Wannafly Winter Camp 2020 Day 7D 方阵的行列式
    [CF1311F] Moving Points
    [CF1311E] Construct the Binary Tree
    [CF1311D] Three Integers
    [CF1311C] Perform the Combo
    [CF1311B] WeirdSort
    [CF1311A] Add Odd or Subtract Even
    Wannafly Winter Camp 2020 Day 7A 序列
    SP7258 SUBLEX
    Wannafly Winter Camp 2020 Day 6J K重排列
  • 原文地址:https://www.cnblogs.com/chensong0524/p/13985677.html
Copyright © 2011-2022 走看看