当用户获得完全控制权时,现有用户会被覆盖-为什么?以及如何删除继承组的访问权限?C#
我有一个包含用户列表的txt文件。我希望授予这些用户对文件/目录的完全访问权限,然后从文件中删除所有其他权限。这是我用来为用户列表提供完全控制的代码:当用户获得完全控制权时,现有用户会被覆盖-为什么?以及如何删除继承组的访问权限?C#,c#,file,permissions,add,C#,File,Permissions,Add,我有一个包含用户列表的txt文件。我希望授予这些用户对文件/目录的完全访问权限,然后从文件中删除所有其他权限。这是我用来为用户列表提供完全控制的代码: var readerAdminUsers = new StreamReader(File.OpenRead(adminFile)); string adminUser = string.Empty; var allAdminUsers = new List<String>(); FileSystemAc
var readerAdminUsers = new StreamReader(File.OpenRead(adminFile));
string adminUser = string.Empty;
var allAdminUsers = new List<String>();
FileSystemAccessRule fsar;
FileSecurity fileSec = new FileSecurity();
while (!readerAdminUsers.EndOfStream)
{
var line = readerAdminUsers.ReadLine();
adminUser = line;
allAdminUsers.Add(adminUser);
fsar = new FileSystemAccessRule(adminUser, FileSystemRights.FullControl, AccessControlType.Allow);
fileSec.AddAccessRule(fsar);
numOfAdmins++;
}
File.SetAccessControl(filePath, fileSec);
我从一个文件开始,例如:-我添加的3个用户
-以及继承的系统和管理员组
最后我想要的是:
-没有用户(除非他们是txt文件中指定的管理员之一)
-没有任何用户组
-以及完全控制文件的管理员
我得到的是:
-3个用户都不见了
-继承的组仍然存在
-管理员用户拥有完全的控制权 请注意,添加拒绝访问不是我要找的,我需要完全删除它们 请帮我修好这个! 非常感谢:)
FileAttributes attr = File.GetAttributes(filePath);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
var security = Directory.GetAccessControl(filePath);
arc = security.GetAccessRules(true, true, typeof(NTAccount));
}
else
{
var security = File.GetAccessControl(filePath);
arc = security.GetAccessRules(true, true, typeof(NTAccount));
}
foreach (FileSystemAccessRule rule in arc)
{
string username = rule.IdentityReference.Value;
if (!allAdminUsers.Contains(username))
{
fileSec.RemoveAccessRule(rule);
File.SetAccessControl(filePath, fileSec);
}
}