C#设置文件夹的访问权限
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规则
var accRule = new FileSystemAccessRule(userName, rights, inherits, propagateToChildren, allowOrDeny);
//修改⽂件夹ACL
dSecurity.ModifyAccessRule(addResetOrRemove, accRule, out result);
//应⽤新的ACL规则到⽂件夹
folder.SetAccessControl(dSecurity);
//返回结果
return result;
}
下⾯这样调⽤
var folderPath = System.AppDomain.CurrentDomain.BaseDirectory;
var userName = "Users";
SetFolderACL(folderPath, userName, FileSystemRights.FullControl, AccessControlType.Allow);
怎么设置文件夹权限

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。