我开发的工具需要授予对其创建的文件“完全控制”访问权限。需要从所有 Windows 帐户甚至未来可能的帐户中读取、修改和删除它。这能实现吗?
我知道我可以为特定用户尝试这个:
FileSystemAccessRule rule = new FileSystemAccessRule(SPECIFIC_USER, FileSystemRights.FullControl, AccessControlType.Allow);
FileSecurity fSecurity = File.GetAccessControl(filePath);
fSecurity.SetAccessRule(rule);
File.SetAccessControl(filePath, fSecurity);
但如何将其授予所有用户呢?甚至未来可能的账户?如果后一部分无法实现,那么如何满足第一个要求?
Thanks.
EDIT:
这是对我有用的代码。取自回答者的链接。
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
这是必需的(链接最后提到的)。它确实向未来的帐户授予特权。
使用此功能的人请注意。
当使用文字字符串时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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)