我开发的工具需要授予对它创建的文件的访问权限"完全控制".它需要从所有windows帐户,甚至future 可能的帐户中读取、修改和删除.这是否可以做到呢?

我知道我可以为一个特定的用户试试这个:

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

但我如何将其授予所有用户?甚至future 可能的账户?如果后一部分是不可能的,那么第一个要求该怎么做呢?

谢谢

编辑:

这就是对我起作用的代码.从应答者的链接中删除.

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(在链接的最后一个部分提到).即使是future 的账户,它也会给予特权.

推荐答案

请使用此软件的用户注意.

当对FileSystemAccessRule使用文字字符串时,它应该是WellKnownSidType.WorldSid而不是"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);
}

.net相关问答推荐

带有ASP.NET核心的Angular 项目模板.API试验

在平板电脑上运行我的 Angular 13 和 .Net Api 项目时,它不会向 api 输入请求

EF Core IEntityTypeConfigurations:从数据库提取数据后相关对象为空

EFCore.DbSet.Update 方法添加新行而不是更新它

与 Datagrid 的 SelectedItem 链接时的 WPF RadioButton 绑定问题

判断 Windows 路径中是否存在可执行文件

在 C# 中生成随机小数

使用 Thread.Abort() 有什么问题

为什么 .NET 内部 Hashtable 中有一个 Thread.Sleep(1)?

将 BitmapImage 转换为 Bitmap,反之亦然

读取方法的属性值

HashSet 是否保留插入顺序?

有没有办法只在 C# 中设置一次属性

C# 测试字符串是否为整数?

加载程序集、查找类和调用 Run() 方法的正确方法

强制 XmlSerializer 将 DateTime 序列化为 'YYYY-MM-DD hh:mm:ss'

无锁多线程适用于真正的线程专家

如何在 Dapper.Net 中编写一对多查询?

如何将 .NET 应用程序编译为本机代码?

Roslyn 编译代码失败