Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I have developed a Console App with CommandLine Args that can display and then remove SACLs from a user inputted Path.
Works great. However, the way I have it setup is that in only displays or removes the SACLS based on the Directory entered.
I need my code to enumerate the Directory and display information not only from the Directory, but also Subdirectories and Files in the directories.

Anyone have this code available or can point me in the right direction?

Thank You!

Here is some sample code of what I have done. Again, it reads in a Directory only. I need the Directory, SubDirectories, and files.

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, 
         typeofSystem.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();
  }
Posted
Updated 17-Feb-11 8:28am
v2
Comments
fjdiewornncalwe 17-Feb-11 14:43pm    
Twdeveloper... Please don't repost the same questions in multiple places. Please either choose this Q&A, or the C# forum. Unfortunately for you, some of us old, anal-retentive types are annoyed by that.

Hi,

You could use recursion to iterate through all subfolders. For example:
- Scan directories using recursion[^]
- http://support.microsoft.com/kb/303974[^]
 
Share this answer
 
Comments
Sreejith Gopinathan 17-Feb-11 14:36pm    
Good answer ..+5
You can go without implementing recursion, because recursive search is already provided using the optional parameter System.IO.SearchOption.AllDirectories:

C#
System.IO.Directory.GetFiles(
    path, searchPattern, System.IO.SearchOption.AllDirectories);
System.IO.Directory.GetDirectories(
    path, searchPattern, System.IO.SearchOption.AllDirectories);


The above usage is all you need to know.
There is one caveat though, related to the searchPattern parameter. Please see this discussion: Directory.Get.Files search pattern problem[^]. At first, I did a mistake, too. All credit goes to Abhishek Sur and DaveyM69.

—SA
 
Share this answer
 
v2
Okay, I am still having issues removing all the Sacl information for Directory, SubDirectory, and files. I tried the recursion and it still just removes the Directory. Also, I attempted to use the Directory.GetFiles( 3 parameters), doesn't like the searchpattern. Read the discussion attached but didn't seem to do the trick.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900