Search code examples
windows-installercustom-action

Set default access permissions on a folder created in a custom installer


I have created a simple custom installer as described here. My new installer works as expected. It creates a folder and adds the path into the app.config file.

The problem is that the read-only flag on the folder gets set and the write permissions for non admin users are removed.

If I manually create the folder and set the permissions, they get reset when the installer is run.

How can I specify these parameters from within my custom installer?

EDIT:

I use the code shown below to set the security permissions for everyone and this is working fine.

// Get the directory info for the existing folder
DirectoryInfo dirInfo = new DirectoryInfo(settingsFileDir);

// Now apply user access permissions to the folder
if (dirInfo != null)
{
    DirectorySecurity security = dirInfo.GetAccessControl();
    security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
    security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
    dirInfo.SetAccessControl(security);
}

When I try to set the folder attributes to remove the read only setting no change is made. I do this with:

dirInfo.Attributes = FileAttributes.Normal;

Solution

  • Using the code below does actually set the access permissions of the folder.

    // Get the directory info for the existing folder
    DirectoryInfo dirInfo = new DirectoryInfo(settingsFileDir);
    
    // Now apply user access permissions to the folder
    if (dirInfo != null)
    {
        DirectorySecurity security = dirInfo.GetAccessControl();
        security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
        security.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
        dirInfo.SetAccessControl(security);
    }
    

    If you view the properties of the folder you can see that the readonly attribute appears to be set but does allow files to be written.

    Test Folder Properties