Q8Y Q8Y - 1 year ago 36
C# Question

How to give permissions for folders in c#?

I need to give the folder "Temporary ASP.NET Files" a write permission using c#... and I use this code to give it the access

DirectoryInfo d1 = new DirectoryInfo(Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "Temporary ASP.NET Files"));
DirectorySecurity md1 = d1.GetAccessControl();

string user_1 = fa.TextGuestDomain + "\\" + fa.TextGuestUser;
md1.AddAccessRule(new FileSystemAccessRule(user_1, FileSystemRights.FullControl,InheritanceFlags.ObjectInherit,PropagationFlags.InheritOnly, AccessControlType.Allow));


When I checked for the security properties for the folder "Temporary ASP.NET Files" after implementing the code, it didn't checked the "write" permission check-box, instead of that it checked the "special permissions" one... I have noticed that even when I changed the access from write to full control or read , it checked the "special permissions" one....

This is not the problem :), the problem is its not giving the right access that i give to it... when I give it write, it doesn't act like if I give it the write permission. I don't know why !! Am I doing it the wrong way ??

when I'm doing it in the manual way its working, while when using the coding way. it's not working...

I hope you can help me with that...

Thanks alot

Answer Source

I know your pain - filesystem ACLs are a pain to modify and even if it seems to work, it might break in some circumstances. In your case, there's a simple solution, fortunately.

The problem lies with PropagationFlags.InheritOnly. This means that this permission is only applied to items that inherit permissions - e.g. you are granting rights only for the files in this directory and not in any subdirectories.

To grant directory rights that inherit "normally" (i.e. propagate to subdirectories and all files), use the following values for InheritanceFlags and PropagationFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit and PropagationFlags.None.