本文整理汇总了C#中System.Security.AccessControl.DirectorySecurity.GetAccessRules方法的典型用法代码示例。如果您正苦于以下问题:C# DirectorySecurity.GetAccessRules方法的具体用法?C# DirectorySecurity.GetAccessRules怎么用?C# DirectorySecurity.GetAccessRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.AccessControl.DirectorySecurity
的用法示例。
在下文中一共展示了DirectorySecurity.GetAccessRules方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
DirectorySecurity ds = new DirectorySecurity(@"C:\Program Files", AccessControlSections.Access);
AuthorizationRuleCollection arc = ds.GetAccessRules(true, true, typeof(NTAccount));
Console.WriteLine("Identity Reference".PadRight(28)+ ": " +
"Access Control Type".PadRight(20) + " " + "File System Rights");
foreach (FileSystemAccessRule fsar in arc)
{
Console.WriteLine(fsar.IdentityReference.ToString().PadRight(28) + ": " +
fsar.AccessControlType.ToString().PadRight(20) + " " + fsar.FileSystemRights);
}
Console.WriteLine();
RegistrySecurity rs = Registry.LocalMachine.GetAccessControl();
arc = rs.GetAccessRules(true, true, typeof(NTAccount));
Console.WriteLine("Identity Reference".PadRight(28) + ": " +
"Access Control Type".PadRight(20) + " " + "Registry Rights");
foreach (RegistryAccessRule rar in arc)
{
Console.WriteLine(rar.IdentityReference.ToString().PadRight(28) + ": " +
rar.AccessControlType.ToString().PadRight(20) + " " + rar.RegistryRights);
}
}
示例2: RemoveAllExplicitAccessRules
/// <summary>
/// Removes all explicit access rules from the supplied directory.
/// </summary>
/// <param name="path">The path to the directory to have access removed on.</param>
/// <param name="security">The DirectorySecurity object of the directory that will be changed.</param>
/// <param name="commitChanges">Indicates whether changes should be commited to this directory. Useful when combining multiple commands.</param>
/// <returns>True if access was removed. False otherwise.</returns>
public static bool RemoveAllExplicitAccessRules(string path, ref DirectorySecurity security, bool commitChanges)
{
// Check whether the path and security object are supplied.
if (!string.IsNullOrEmpty(path) && security != null)
{
// Check whether the directory exists.
if (SystemDirectory.Exists(path))
{
// A path and security object are supplied.
// Remove existing explicit permissions.
security = GetSecurityObject(path);
AuthorizationRuleCollection rules = security.GetAccessRules(true, false, typeof(SecurityIdentifier));
foreach (AuthorizationRule rule in rules)
{
security.RemoveAccessRule((FileSystemAccessRule)rule);
}
// Commit the changes if necessary.
if (commitChanges)
{
try
{
SystemDirectory.SetAccessControl(path, security);
}
catch (UnauthorizedAccessException)
{
// The current process does not have access to the directory specified by path.
// Or the current process does not have sufficient privilege to set the ACL entry.
return false;
}
catch (PlatformNotSupportedException)
{
// The current operating system is not Windows 2000 or later.
return false;
}
}
return true;
}
else
{
// The directory does not exist.
return false;
}
}
else
{
// A path and security object were not supplied.
return false;
}
}