如何为所有用户授予我的应用程序创建的文件的完全权限?

如何为所有用户授予我的应用程序创建的文件的完全权限?

问题描述:

我开发的工具需要授予访问权限完全控制";到它创建的文件.它需要从所有 Windows 帐户甚至可能的未来帐户中读取、修改和删除.这能实现吗?

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 possible future accounts. Could this be achieved?

我知道我可以为特定用户尝试此操作:

I know I can try this for a SPECIFIC_USER:

FileSystemAccessRule rule = new FileSystemAccessRule(SPECIFIC_USER, FileSystemRights.FullControl, AccessControlType.Allow);
FileSecurity fSecurity = File.GetAccessControl(filePath);
fSecurity.SetAccessRule(rule);
File.SetAccessControl(filePath, fSecurity);

但我如何将其授予所有用户?甚至可能的未来账户?如果后面的部分不可能,那么第一个要求怎么办?

But how do I grant it to all users? And even possible future accounts? If the latter part is not possible, how to go about the first requirement?

谢谢.

这是对我有用的代码.摘自回答者的链接.

This is the code which worked for me. Taken from the answerer's link.

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);
}

注意 PropagationFlags.NoPropagateInherit 这是必需的(在链接的最后一个提到).它确实为未来的帐户授予特权.

Note the PropagationFlags.NoPropagateInherit which is required (mentioned towards the last in the link). It does grant privilege to even future accounts.

使用此工具的注意事项.

Note to people using this.

当为 FileSystemAccessRule 使用文字字符串时,它应该是 WellKnownSidType.WorldSid 而不是 "everyone".

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

原因是因为有多种Window语言,而Everyone只适用于EN语言,所以对于西班牙语,它可能是Todos"(或其他).

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);
}