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

标签 c# .net winforms permissions user-accounts

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

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

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"

原因是因为有多种 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);
}

关于c# - 如何授予所有用户对我的应用程序创建的文件的完全权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9108399/

相关文章:

C# MySql.Data.MySqlClient 创建数据库失败

c# - C# 中的搜索按钮

c# - 为什么将 ComboBox.SelectedValue 设置为 null 会导致 ArgumentNullException?

c# - 有没有一种方法可以在 TreeView.Nodes 集合中搜索 TreeNode.Text 字段?

c# - 使用内部逻辑创建自定义属性

c# - 具有 O(1) 查找时间的数据结构将允许重复

c# - WCF 从 System.SecurityException 中剥离详细信息

c# - 在类库中获取 UI 调度程序

c# - linq c#中的动态字符串

c# - 如何获取多维数组的宽和高?