我开发的工具需要授予对它创建的文件的“完全控制”访问权限。它需要从所有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);
但是我如何将它授予所有用户呢?甚至是未来可能的账户?如果后半部分是不可能的,那么如何着手第一个需求呢?
谢谢。
编辑:
这就是为我工作的代码。取自回答者的链接。
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
(在链接的最后提到)。即使是将来的帐户,它也会授予特权。
发布于 2013-04-25 21:51:01
使用此工具的用户请注意。
当对FileSystemAccessRule
使用文本字符串时,它应该是WellKnownSidType.WorldSid
而不是"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);
}
发布于 2012-02-02 15:17:24
您需要完全控制计算机上的"Everyone“组。在MSDN上找到了关于它的this帖子。
希望这对你有用。
https://stackoverflow.com/questions/9108399
复制相似问题