我开发的工具需要将访问权限"完全控制"授予由它创建的文件.需要从所有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哪个是必需的(在链接的最后面提到).它确实为未来的账户授予特权.
我正在使用C#StreamWritier类.问题:
我正在创建这样的文件:
private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.FileName = textBox1.Text;
save.Filter = "Text File | *.rtf";
if (save.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(save.OpenFile());
writer.WriteLine(textBox2.Text);
}
writer.Dispose();
writer.Close();
}
Run Code Online (Sandbox Code Playgroud)