0

This is what I've done so far:

class Program
{

    static void Main(string[] args)
    {

        DirectoryInfo startDirectory = new DirectoryInfo(@"C:\Users\Angelo\Desktop\ExerciseTest");

        string output = "";

        DirectoryInfo[] subDirectoryes = startDirectory.GetDirectories();

        for (int i = 0; i < subDirectoryes.Length; i++)
        {
            output = output + subDirectoryes[i].ToString() + "\r\n";
        }

        Console.WriteLine(output);

        Console.ReadLine();

    }
}

This gives me as a result the first subfolders of the specified folder, the problem is that I need to find all the subfolders,subsubfolders,subsubsubfolders etc.. and files in the specified folder, and then output them with this indentation:

  • Subfolder1
    • SubSubfolder1
      • SubSubSubfolder1
      • SubSubfolderFile1
    • SubSubfolder2
  • Subfolder2
    • SubfolderFile1

I've been trying to do it many times but I can't figure out how, maybe I'm missing some command (this is the first time I program with c# System.IO) can you please give me some tips or tell me what commands I should use? I'm going crazy.

Thielicious
  • 4,122
  • 2
  • 25
  • 35

2 Answers2

2

There is an overload of GetDirectories you should use that allows you to define the search scope:

DirectoryInfo[] subDirectoryes = 
              startDirectory.GetDirectories("*", SearchOption.AllDirectories);

This will get all directories, including all the subdirectories all the way down.

The first argument is a search text filter, "*" will find all folders.

A complete example with indentation:

void Main()
{
    var path = @"C:\Users\Angelo\Desktop\ExerciseTest";
    var initialDepth = path.Split('\\').Count();

    DirectoryInfo startDirectory = new DirectoryInfo(path);

    StringBuilder sb = new StringBuilder();

    DirectoryInfo[] subDirectoryes = startDirectory.GetDirectories("*", SearchOption.AllDirectories);

    for (int i = 0; i < subDirectoryes.Length; i++)
    {
        var level = subDirectoryes[i].FullName.Split('\\').Count() - initialDepth;
        sb.AppendLine($"{new string('\t', level)}{subDirectoryes[i].Name}");
    }

    Console.WriteLine(sb.ToString());

    Console.ReadLine();
}
Peter Bons
  • 26,826
  • 4
  • 50
  • 74
0

In order to go through all the subfolders, you need recursive function. Basically, you have to repeat the procedure that you apply currently only for root directory to all the directories that you encounter.

Edit: Had to take a little while to provide this code example:

class Program
{
    static void Main(string[] args)
    {
        var basePath = @"C:\Users\Angelo\Desktop\ExerciseTest";

        DirectoryInfo startDirectory = new DirectoryInfo(basePath);
        IterateDirectory(startDirectory, 0);

        Console.ReadLine();
    }

    private static void IterateDirectory(DirectoryInfo info, int level)
    {
         var indent = new string('\t', level);
         Console.WriteLine(indent + info.Name);
         var subDirectories = info.GetDirectories();

         foreach(var subDir in subDirectories)
         {
             IterateDirectory(subDir, level + 1);
         }
     }
 }

A bit of an explanation, what the IterateDirectory does (as requested):

  1. It prints the directory name with indentation, which is dependent on current level in the directory subtree.
  2. For each of the directories in current directory: call the IterateDirectory method with level increased by one.

This is a rather standard approach for going through tree-like structures.

Jakub Jankowski
  • 731
  • 2
  • 9
  • 20
  • Why would you advice this as there is build-in support in the framework to get all directories, including all the subdirectories all the way down? – Peter Bons Nov 12 '17 at 13:53
  • @PeterBons - There are times when you want to prune subfolders as you traverse down. Knowing how to do this kind of recursive descent is worthwhile. It would be good if the code were included in this answer. – Enigmativity Nov 12 '17 at 13:55
  • Thank you so much, you're my hero. –  Nov 12 '17 at 14:13
  • One last thing, could you please explain to me what IterateDIrectory does and how can i also find the files and organize them? Or if you have time, maybe explain what reasoning you did to get the code, i really want to learn. Thanks a lot again –  Nov 12 '17 at 14:37
  • I've added a simple explanation of the method in the original post. I had to deal with such tasks in the past, that's why I had an answer in my head. As far as addressing other issues goes - I believe that neither comments nor posts are suitable for such discussion, chat would probably a better place for thath – Jakub Jankowski Nov 12 '17 at 15:01