Domanda

I'm trying to change the permissions for a directory. To do this I am running an elevated process that actually performs the SetAccessControl.

    static void Main(string[] args)
    {
        var options = new Options();
        if (!CommandLine.Parser.Default.ParseArguments(args, options)) return;

        var myDirectoryInfo = new DirectoryInfo(options.folder);
        var myDirectorySecurity = myDirectoryInfo.GetAccessControl();
        var usr = options.user;

        myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(usr, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));

        try
        {
            myDirectoryInfo.SetAccessControl(myDirectorySecurity);
        }
        catch (Exception ex)
        {               
           Console.WriteLine(ex.Message);
        }

    }

When I run this as administrator there are no errors, but the folder access permissions remain unchanged. Any ideas what is wrong?

È stato utile?

Soluzione

The answer that worked for me was using ModifyAccessRule to first grant permissions to the directory. Then to add the inheritance rules.

Also I found that the windows explorer is not always showing the current permissions, not sure what causes it to refresh, but I noticed that at times the permissions were set properly, and my program could access the files in directory,even though explorer showed no permission.

    private static bool SetAccess(string user, string folder)
    {
        const FileSystemRights Rights = FileSystemRights.FullControl;

        // *** Add Access Rule to the actual directory itself
        var AccessRule = new FileSystemAccessRule(user, Rights,
            InheritanceFlags.None,
            PropagationFlags.NoPropagateInherit,
            AccessControlType.Allow);

        var Info = new DirectoryInfo(folder);
        var Security = Info.GetAccessControl(AccessControlSections.Access);
        bool Result;

        Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);

        if (!Result) return false;

        // *** Always allow objects to inherit on a directory
        const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

        // *** Add Access rule for the inheritance
        AccessRule = new FileSystemAccessRule(user, Rights,
            iFlags,
            PropagationFlags.InheritOnly,
            AccessControlType.Allow);

        Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);

        if (!Result) return false;

        Info.SetAccessControl(Security);

        return true;
    } 

Altri suggerimenti

Use the following instead of myDirectoryInfo.SetAccessControl(myDirectorySecurity);

try
{
    Directory.SetAccessControl(options.folder,myDirectorySecurity);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top