【问题标题】:How to grant full permission to a file created by my application for ALL users?如何授予所有用户对我的应用程序创建的文件的完全权限?
【发布时间】:2012-02-24 21:00:43
【问题描述】:

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

我知道我可以为 SPECIFIC_USER 试试这个:

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 是必需的(在链接的最后提到)。它确实为未来的帐户授予特权。

【问题讨论】:

  • 请注意,不要使用“每个人”,而是使用返回 SecurityIdentifier 对象的new SecurityIdentifier(WellKnownSidType.WorldSid, null)。每个人都只适用于英文 Windows 安装,使用其他方法确保它与多个语言版本兼容。
  • @trukin 你能回答吗?谢谢
  • @nawfal:我也遇到了同样的问题,一旦安装了应用程序,我需要授予对安装文件夹的访问权限,但是我在哪里可以编写此代码?
  • @HinaKhuman 给予安装文件夹权限由安装程序更好地处理。我不知道您使用的是哪一个,但它应该很简单。如果您想从 C# 中执行此操作,请从您想要的任何位置调用 GrantAccess 方法,但您的应用程序本身应该拥有权限。
  • @nawfal:谢谢!在此处查看详细问题:stackoverflow.com/q/48165315/5743676

标签: c# .net winforms permissions user-accounts


【解决方案1】:

使用此功能的人请注意。

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

原因是因为 Window 语言有多种,Everyone 只适用于 EN 语言,所以对于西班牙语,它可能是“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 文件的权限(因为我遇到了只读错误)。谢谢!
  • 请问返回值的用途?
  • @hypehuman 哦,真的没有,它本来是要从某个地方调用的,如果它失败了(比如 GrantAccess 捕获了一个异常,那么它将返回 false),那么无论使用什么代码都不应继续,因为没有已授予权限。
  • DirectorySecurity 未找到。什么是参考库?我添加了 3 行“使用...”,仍然错误。
  • 不要忘记包含InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit,否则它只会添加用户/组而不对其应用任何权限(用户/组将只有特殊权限)。刚刚花了一个小时试图了解为什么它会向组应用任何权限,所以希望这可以节省一些时间!
【解决方案2】:

您需要完全控制计算机上的“所有人”组。在 MSDN 上找到了 this 的帖子,其中谈到了它。

希望这对你有用。

【讨论】:

  • 谢谢,我会注意的。这是否会授予对未来帐户的访问权限?
  • 感谢它确实有效,并授予对未来用户帐户的访问权限。请接受我的编辑,以便其他人知道应该做什么。
猜你喜欢
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
  • 2016-10-21
相关资源
最近更新 更多