CSDN 设置文件夹权限

/*
需要添加以下命名空间:
using System.IO;
using System.Security.AccessControl;
*/

string sPath = Server.MapPath(文件夹名称字符串);
Directory.CreateDirectory(sPath);
addpathPower(sPath, "ASPNET", "FullControl");

//

public void addpathPower(string pathname, string username, string power)
{

    DirectoryInfo dirinfo = new DirectoryInfo(pathname);

    if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
    {
        dirinfo.Attributes = FileAttributes.Normal;
    }

    //取得访问控制列表
    DirectorySecurity dirsecurity = dirinfo.GetAccessControl();

    switch (power)
    {
        case "FullControl":
            dirsecurity.AddAccessRule(new FileSystemAccessRule(uername, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
            break;
        case "ReadOnly":
           dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
            break;
        case "Write":
            dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
            break;
        case "Modify":
            dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));
            break;
    }
    dirinfo.SetAccessControl(dirsecurity);
}

本文出处:http://blog.csdn.net/LiveStar/archive/2008/07/20/2680734.aspx

==========================================================================

// 获取 指定目录 除Administrators和SYSTEM之外的 权限列表
         public List GetDirectoryAccountSecurity(string DirName)
         {
             List dAccount = new List();
             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 AUTHORITYSYSTEM" && rule.IdentityReference.Value != @"BUILTINAdministrators")
                         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);
             }
         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Ubuntu 20.04中修改文件夹权限可以通过使用chmod命令来实现。首先,打开终端并导航到要修改权限文件夹所在的位置。然后,使用以下命令来修改文件夹权限: ``` sudo chmod <权限设置> <文件夹名称> ``` 其中,<权限设置>是一个三位数的数字,代表了文件夹权限。每一位数字分别代表了所有者、所属组和其他用户的权限。常见的权限设置包括: - 7:读、写和执行权限 - 6:读和写权限 - 5:读和执行权限 - 4:只读权限 - 0:无权限 例如,如果要将文件夹权限设置为所有者具有读、写和执行权限,所属组和其他用户只有读权限,可以使用以下命令: ``` sudo chmod 755 <文件夹名称> ``` 这将修改文件夹权限为rwxr-xr-x。请注意,使用sudo命令需要管理员权限,因此您可能需要输入管理员密码。 引用\[1\]中提到了在Ubuntu 19.04中启动Nautilus的问题,引用\[2\]中提到了重新打开Nautilus可能会耗时很久的情况。这些引用与修改文件夹权限的问题没有直接关联,因此在回答问题时不需要引用这些内容。 #### 引用[.reference_title] - *1* *2* *3* [Ubuntu 20.04桌面文件夹图标消失及文件系统无法打开](https://blog.csdn.net/Nismilesucc/article/details/125334485)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值