naw*_*fal 70 .net c# permissions user-accounts winforms
我开发的工具需要将访问权限"完全控制"授予由它创建的文件.需要从所有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
哪个是必需的(在链接的最后面提到).它确实为未来的账户授予特权.
Ang*_*gas 118
使用此功能的人请注意.
当使用文字字符串时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);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
71312 次 |
最近记录: |