Twitter Delicious Facebook Digg Stumbleupon Favorites More

Thứ Hai

Removing SACLS from SubDirectories and Files....


I have the code in place to have a User input a Directory (ex C:\TestFolder) and can either use 2 arguments, SEARCH or CLEAN. Which either displays all the SACLS or Removes them from the directory.
Now my code is only working at the top level directory. Would really like it to scan and display all Sacls for the SubFolders and Files of (C:\TestFolder). Of course have it remove all SACLS in the other ARG.
Been having trouble trying to get the subdirectory and file information in place.
Looking for code to add to existing code to include SubDirectory and File info.
Yeah, so basically, my code looks something like this:



class Program
{
  
static void Main(string[] args)
{
  
try
{
  
if (args[0].ToUpper().Trim() == "SEARCH") // Displays all Sacls for path
{
Console.WriteLine("Enter your Directory path: ex. C:\\TestFolder");
string path = Console.ReadLine();
Console.WriteLine("{0}", path);
  
if (Directory.Exists(path))
{
Console.WriteLine(GetDirectoryAuditControlInformation(path));
}
else
{
Console.WriteLine("Path does not exist");
}
}
else if // argument for == CLEAN which removes all Sacls. 
  
// my function for GetDirectoryAuditControlInformation(string path)
public static string GetDirectoryAuditControlInformation(string path)
{
StringBuilder info = new StringBuilder();
info.AppendLine("SACL entries for the path \ "" + path + "\":");
info.AppendLine();
DirectorySecurity dsecurity = Directory.GetAccessControl(path, AccessControlSections.Audit); 
AuthorizationRuleCollection acl = dsecurity.GetAuditRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (FileSystemAuditRule ace in acl)
{
string aceInfo = GetAuditAceInformation(ace);
info.AppendLine(aceInfo);
}
return info.ToString();
}
  
public static string GetAuditInformation(FileSystemAuditRule ace)
{
StringBuilder info = new StringBuilder();
string line = string.Format("Account: {0}", ace.IdentityReference.Value);
info.AppendLine(line);
line = string.Format("Type: {0}", ace.AuditFlags);
info.AppendLine(line);
line = string.Format("Rights: {0}", ace.FileSystemRights);
info.AppendLine(line);
line = string.Format("Inherited ACE: {0}", ace.IsInherited);
info.AppendLine(line);
return info.ToString();

0 nhận xét:

Đăng nhận xét

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Blogger Templates