C#修改文件夾權限


/// <summary>
///為文件夾添加users,everyone用戶組的完全控制權限
/// </summary>
/// <param name="dirPath"></param>
static void AddSecurityControll2Folder(string dirPath)
{
    //獲取文件夾信息
    DirectoryInfo dir = new DirectoryInfo(dirPath);
    //獲得該文件夾的所有訪問權限
    System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
    //設定文件ACL繼承
    InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    //添加ereryone用戶組的訪問權限規則 完全控制權限
    FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
    //添加Users用戶組的訪問權限規則 完全控制權限
    FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
    bool isModified = false;
    dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
    //設置訪問權限
    dir.SetAccessControl(dirSecurity);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM