CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2010
    Posts
    22

    Working with Directories and AccessControl

    Hello,

    I am in search of some assistance with this code I am working on.

    I have developed a Console Application with 2 CommandLine Args, Search Directory for Sacls and Display and search Directory for Sacls and Remove.

    Now I have it set up where the user can input which directory and it works fine. Only problem is is only removes Sacls at the inputted Path. Meaning, any Subdirectories or recursive files do not get displayed or Sacls do not get removed.

    I have been attempted to enter the code for Directory.GetDirectories, Directory.SearchOptions, etc. Cannot seem to gather the data for the Subs and Files as well.

    Any assistance would be appreciated.

    Thank You!

  2. #2
    Join Date
    Sep 1999
    Posts
    137

    Re: Working with Directories and AccessControl

    Perhaps the reason you have no replies is that it is hard to understand exactly what you are asking.

    I believe the question is "Given a directory, how do I find the subdirectories and files?"

    Since you have tried Directories.GetDirectories, perhaps you can tell us more about what you have tried and what you do get? Error messages? Empty results?

    Have you tried GetFileSystemInfos?

  3. #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