CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #3
    Join Date
    Aug 2010
    Posts
    22

    Re: Working with Directories and AccessControl

    Sorry. I realize my post may have been a little confusing. Was trying to get a conversation started without putting too much code in first.

    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();
    }
    Last edited by TWDeveloper; February 3rd, 2011 at 02:45 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured