C# Privilege issues creating folders from Windows service running as Local System account.

664 views Asked by At

I have a service running as Local System. I'm creating some folders from the service. These folders are created which don't give people in the "Users" group in windows 7 write access.

How can I create these folders which grant everyone write access?

1

There are 1 answers

0
Mauricio Gracia Gutierrez On

You can use the logic that I found in this example

public class Permissions
{
    public void addPermissions(string dirName, string username)
    {
        changePermissions(dirName, username, AccessControlType.Allow);
    }

    public void revokePermissions(string dirName, string username)
    {
        changePermissions(dirName, username, AccessControlType.Deny);
    }

    private void changePermissions(string dirName, string username, AccessControlType newPermission)
    {
        DirectoryInfo myDirectoryInfo = new DirectoryInfo(dirName);

        DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();

        string user = System.Environment.UserDomainName + "\\" + username;

        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(
            user, 
            FileSystemRights.Read | FileSystemRights.Write | FileSystemRights.ExecuteFile | FileSystemRights.Delete, 
            InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, 
            PropagationFlags.InheritOnly, 
            newPermission
        ));

        myDirectoryInfo.SetAccessControl(myDirectorySecurity);
    }
}

I also found this article that has other examples

http://www.c-sharpcorner.com/uploadfile/babu_2082/adding-groups-user-names-and-permissions-for-a-directory-in-C-Sharp/