Search code examples
c#.netdirectoryprivilegeselevated-privileges

C# Edit file system directory privileges from admin process


I need to allow edit files in directory to users. Directory and files in it was created with admin priviliges. So, when user try to resave files in it - exception. What I can do in this case? Or may be exist a way to create directory with specific rights? I am using C# .Net 4.0


Solution

  • Grand access for all users (with subfolders) like this:

    DirectoryInfo dInfo;
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
    dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));
    dInfo.SetAccessControl(dSecurity);