Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

d1.SetAccessControl(md1);

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 ??

Note: 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

like image 910
Q8Y Avatar asked May 23 '11 09:05

Q8Y


People also ask

How do I give permission to a folder and subfolders?

chmod 755 {} specifies the command that will be executed by find for each directory. chmod 644 {} specifies the command that will be executed by find for each file. {} is replaced by the path. ; the semicolon tells find that this is the end of the command it's supposed to execute.

How do I give permission to all files in a folder?

Changing permissions with chmod To modify the permission flags on existing files and directories, use the chmod command ("change mode"). It can be used for individual files or it can be run recursively with the -R option to change permissions for all of the subdirectories and files within a directory.


2 Answers

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.

like image 95
Sander Avatar answered Sep 30 '22 16:09

Sander


private static void GrantAccess(string file)
{
    bool exists = System.IO.Directory.Exists(file);
    if (!exists)
    {
        DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
        Console.WriteLine("The Folder is created Sucessfully");
    }
    else
    {
        Console.WriteLine("The Folder already exists");
    }
    DirectoryInfo dInfo = new DirectoryInfo(file);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
    dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
    dInfo.SetAccessControl(dSecurity);
   
}

The above code will set the access rights of the folder to full control/ read-write to every user (everyone).

like image 40
Richendra kumar ravi Avatar answered Sep 30 '22 18:09

Richendra kumar ravi