如何拒绝C#中所有用户对文件夹的写权限?

时间:2016-08-01 19:36:11

标签: c#

我想拒绝C#中特定文件夹的写入权限,我找到了检查权限和允许权限的方法。

DirectoryInfo dInfo = new DirectoryInfo(fullPath);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.FullControl,
                                                 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,

但是没有方法可以检查或拒绝C#

中的权限

尝试了这段代码,但我想让管理员拥有对文件夹的完全访问权限。

DirectoryInfo dInfo = new DirectoryInfo(@"C:\New folder\New folder");
        DirectorySecurity dSecurity = dInfo.GetAccessControl();
        dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), 
            FileSystemRights.Write, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, 
            PropagationFlags.NoPropagateInherit, AccessControlType.Deny));
        dInfo.SetAccessControl(dSecurity);

1 个答案:

答案 0 :(得分:2)

我认为您需要在添加规则后使用路径和DirectorySecurity变量调用Directory.SetAccessControl