Vivek Nuna
Vivek Nuna

Reputation: 1

How to deny write permission to a folder for all users in C#?

I want to deny write permission to a particuler folder in C#, I have found methods which checks for permissions and for allow permissions.

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

But there is no method to check or to deny permission in C#

Tried this code, but I want to allow admin to have full access to folder.

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);

Upvotes: 2

Views: 908

Answers (1)

Zenny
Zenny

Reputation: 181

I believe you need to call Directory.SetAccessControl using the path and your DirectorySecurity variable after you add rules.

Upvotes: 3

Related Questions