我开发的工具需要授予对其创建的文件的“完全控制”访问权限。它需要从所有 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/