我开发的工具需要授予对它创建的文件的访问权限"完全控制".它需要从所有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 的账户,它也会给予特权.