相关疑难解决方法(0)

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

我开发的工具需要将访问权限"完全控制"授予由它创建的文件.需要从所有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);
Run Code Online (Sandbox Code Playgroud)

但是如何将其授予所有用户?甚至可能的未来帐户?如果后一部分不可能,如何进行第一项要求?

谢谢.

编辑:

这是适合我的代码.取自回答者的链接.

private bool 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);
    return true;
}
Run Code Online (Sandbox Code Playgroud)

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

.net c# permissions user-accounts winforms

70
推荐指数
2
解决办法
7万
查看次数

标签 统计

.net ×1

c# ×1

permissions ×1

user-accounts ×1

winforms ×1