Click here to Skip to main content
15,668,126 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hello,

In the following code (see code below) I am attempting to convert the function " public static string GetDirectoryAuditControlInformation(string path)" into a Boolean Function.

Instead of, "return info.ToString();", I would like return TRUE is String aceinfo contains information and FALSE if String aceinfo is empty (Meaning the path contained zero SACL information.)

I know that the Function needs to be changed to public static bool, instead of static string. In my attempts I am just not getting the results I need.

If anyone could lend some assistance to this I would greatly appreciate it.

Thank You!

C#
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 22-Feb-11 7:35am
v6
Comments
Sergey Alexandrovich Kryukov 22-Feb-11 13:16pm    
Please format you code some more to avoid wrapping of long lines (if you don't see wrapping while editing, this is good enough).
--SA

1 solution

Something like:
C#
public static bool GetDirectoryAuditControlInformation(string path)
{
  bool result = false;  // assume no SACL info
  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);
   if (aceinfo != null)
   {
     // found SACL info so set result .TRUE.
     result = true;
     info.AppendLine(aceInfo);
   }
  }
 return result;
 }
 
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