首先添加对System.Security.AccessControl的引用
1.下面是对文件的处理:
public void 允许文件从父继承(string fileFullName)
{
FileSecurity fs1 = System.IO.File.GetAccessControl(fileFullName);
//添加管理员并完全控制
fs1.AddAccessRule(new FileSystemAccessRule("administrator", FileSystemRights.FullControl, AccessControlType.Allow));
System.IO.File.SetAccessControl(fileFullName, fs1);
//取消继承
fs1.SetAccessRuleProtection(false, true);
System.IO.File.SetAccessControl(fileFullName, fs1);
}
public void 取消文件从父继承(string fileFullName)
{
FileSecurity fs1 = System.IO.File.GetAccessControl(fileFullName);
fs1.SetAccessRuleProtection(true, false);
System.IO.File.SetAccessControl(fileFullName, fs1);
}
2.下面是对目录的处理
public void 允许目录从父继承(string dirFullPath)
{
DirectorySecurity fs1 = System.IO.Directory.GetAccessControl(dirFullPath);
//添加管理员并完全控制
fs1.AddAccessRule(new FileSystemAccessRule("administrator", FileSystemRights.FullControl, AccessControlType.Allow));
System.IO.Directory.SetAccessControl(dirFullPath, fs1);
//取消继承
fs1.SetAccessRuleProtection(false, true);
System.IO.Directory.SetAccessControl(dirFullPath, fs1);
}
public void 取消目录从父继承(string dirFullPath)
{
DirectorySecurity fs1 = System.IO.Directory.GetAccessControl(dirFullPath);
fs1.SetAccessRuleProtection(true, false);
System.IO.Directory.SetAccessControl(dirFullPath, fs1);
}