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

88

我正在开发的工具需要为它创建的文件授予"完全控制"访问权限。这个文件需要可以被所有Windows账户(包括未来的账户)读取、修改和删除。这个目标能够实现吗?

我知道我可以尝试为一个特定的用户实现这个目标:

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

但是如何将其授予所有用户?甚至是未来可能存在的账户?如果后者不可能,那么如何满足第一个要求?

谢谢。

编辑:

这是我用于解决问题的代码。取自回答者的链接。

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,这是必需的。它甚至授予未来帐户特权。


18
提醒大家,不要使用“everyone”,而应该使用 new SecurityIdentifier(WellKnownSidType.WorldSid, null),该方法会返回一个SecurityIdentifier对象。"Everyone" 只适用于英语 Windows 系统安装版,使用另一种方法可以确保其兼容多种语言版本。 - Angelo Vargas
@trukin,你能把它变成一个答案吗?谢谢。 - nawfal
@nawfal:我也遇到了同样的问题,我需要在应用程序安装后给予访问我的安装文件夹的权限,但是我应该在哪里编写这段代码呢? - Hina Khuman
@HinaKhuman 给予安装文件夹权限最好由安装程序处理。我不知道你使用的是哪一个,但应该很简单。如果你想从C#中进行操作,则可以在任何地方调用GrantAccess方法,但你的应用程序本身应该具有权限。 - nawfal
@nawfal:谢谢!请查看此处的详细问题:https://stackoverflow.com/q/48165315/5743676 - Hina Khuman
3个回答

141

使用 FileSystemAccessRule 的字面字符串时,应该使用 WellKnownSidType.WorldSid 而不是 "everyone"

原因是因为存在多种Windows语言和 "Everyone" 只适用于英文版本,因此对于西班牙语可能是 "Todos"(或其他)。

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

非常感谢您。一直在努力解压文件并将权限设置为.mdf文件(因为我遇到了只读错误)。谢谢! - CularBytes
我可以问一下返回值的作用吗? - hypehuman
@hypehuman 噢,其实没有什么特别的,它是要从某个地方调用的,如果它失败了(比如GrantAccess捕获了一个异常,那么它就会返回false),那么使用它的任何代码都不应该继续执行,因为没有授予权限。 - Angelo Vargas
DirectorySecurity未找到。参考库是什么?我添加了3行“using...”,但仍然出现错误。 - anhtv13
2
不要忘记包括 InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit,否则它只会添加用户/组而不应用任何权限(用户/组将仅具有特殊权限)。刚刚花了一个小时才明白为什么它不会向组应用任何权限,所以希望这可以节省其他人的时间! - Kappacake

13
你需要在机器上将“Everyone”组授予完全控制权限。在MSDN上找到了这篇文章,其中讨论了这个问题。
希望这对你有用。

谢谢,我会处理的。这是否授予对未来账户的访问权限? - nawfal
谢谢,它确实起作用了,并授予了对未来用户帐户的访问权限。请接受我的编辑,以便其他人知道应该做什么。 - nawfal

0

这里有类似的代码,但只限于处理单个文件,这也是我来到这里的原因。但为了更好的安全性,建议使用WellKnownSidType.AuthenticatedUserSid而不是WordSid。

var fileSecurity = new System.Security.AccessControl.FileSecurity();
var everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var rule = new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Allow);
fileSecurity.AddAccessRule(rule);
  
File.SetAccessControl(path, fileSecurity);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接