/// <summary>
    /// 为访问的文件分配权限
    /// </summary>
    /// <param name="pathname">文件路径</param>
    /// <param name="username">赋予的username,类似"ASPNET"</param>
    /// <param name="power">所赋予的权限</param>
    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(username, 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;

 

 

 

 
        }
    }

相关文章:

  • 2021-06-09
  • 2021-10-07
  • 2022-02-16
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2021-08-10
  • 2021-08-18
猜你喜欢
  • 2021-08-27
  • 2021-08-22
  • 2021-09-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案