本文整理汇总了C#中System.Security.AccessControl.DirectorySecurity.RemoveAccessRuleSpecific方法的典型用法代码示例。如果您正苦于以下问题:C# DirectorySecurity.RemoveAccessRuleSpecific方法的具体用法?C# DirectorySecurity.RemoveAccessRuleSpecific怎么用?C# DirectorySecurity.RemoveAccessRuleSpecific使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.AccessControl.DirectorySecurity
的用法示例。
在下文中一共展示了DirectorySecurity.RemoveAccessRuleSpecific方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RemoveAllAccessRules
/// <summary>
/// Removes all access rules from the supplied directory.
/// </summary>
/// <param name="path">The path to the directory to remove all access rules from.</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 all rules were removed. False if an error occurred.</returns>
public static bool RemoveAllAccessRules(string path, ref DirectorySecurity security, bool commitChanges)
{
// Check whether a path and security object were supplied.
if (!string.IsNullOrEmpty(path) && security != null)
{
// A path and security object were supplied.
// Check whether the path exists.
if (SystemDirectory.Exists(path))
{
// The directory exists.
try
{
// Get all the authorization rules for the directory.
AuthorizationRuleCollection ruleCollection = security.GetAccessRules(true, true, typeof(SecurityIdentifier));
// Remove all the authorization rules for the entry.
foreach (FileSystemAccessRule rule in ruleCollection)
{
security.RemoveAccessRuleSpecific(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;
}
catch
{
// There was an error removing the rules.
return false;
}
}
else
{
// The directory does not exist.
return false;
}
}
else
{
// An directory or security object were not supplied.
return false;
}
}