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規(guī)則 var accRule = new FileSystemAccessRule(userName, rights, inherits, propagateToChildren, allowOrDeny); //修改文件夾ACL dSecurity.ModifyAccessRule(addResetOrRemove, accRule, out result); //應用新的ACL規(guī)則到文件夾 folder.SetAccessControl(dSecurity); //返回結果 return result; } 調(diào)用方法: var folderPath = System.AppDomain.CurrentDomain.BaseDirectory; var userName = "Users"; SetFolderACL(folderPath, userName, FileSystemRights.FullControl, AccessControlType.Allow); 該文章在 2023/9/10 11:58:56 編輯過 |
關鍵字查詢
相關文章
正在查詢... |