How to grant full permission to a file created by my application for ALL users?

后端 未结 2 462
我寻月下人不归
我寻月下人不归 2020-11-28 23:42

The tool I develop needs to grant access rights \"Full Control\" to a file created by it. It needs to be read, modified and deleted from all windows accounts and even possib

相关标签:
2条回答
  • 2020-11-28 23:56

    You will need to give full control to "Everyone" group on the machine. Found this post on MSDN which talks about it.

    Hope this works for you.

    0 讨论(0)
  • 2020-11-29 00:15

    Note to people using this.

    When using literal strings for the FileSystemAccessRule, it should be WellKnownSidType.WorldSid instead of "everyone".

    The reason is because there are multiple Window languages and Everyone only applies to EN ones, so for Spanish, it might be "Todos" (or something else).

    using System.Security.AccessControl;
    using System.Security.Principal;
    using System.IO;
    
    private void GrantAccess(string fullPath)
    {
        DirectoryInfo dInfo = new DirectoryInfo(fullPath);
        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);
    }
    
    0 讨论(0)
提交回复
热议问题