S
S
Sergey Tryapkin2018-06-05 16:10:14
recursion
Sergey Tryapkin, 2018-06-05 16:10:14

Tree generation for directories, recursion?

Good afternoon.
Need help writing a tree generation method for creating directories.
The user specifies the depth and amount per level.
I can't figure out how to do this?
An example of what is required:
Depth: 3 .
Quantity per level: 3 .
On an output I want to receive:
C:\folder_1;
C:\folder_1\folder_1_1;
C:\folder_1\folder_1_1\folder_1_1_1;
C:\folder_1\folder_1_1\folder_1_1_2;
C:\folder_1\folder_1_1\folder_1_1_3;
C:\folder_1\folder_1_2;
etc.
I would appreciate any advice or links!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Ogoun Er, 2018-06-05
@Triapkin

var list = new List<string>();
FillFolderList("C:\\", list, 0, 3, 3);


public static void FillFolderList(string path, List<string> list, int current_level, int depth, int width)
{
    list.Add(path);
    if (current_level >= depth) return;
    int current_width = 0;
    try
    {
        foreach (var subdir in Directory.GetDirectories(path))
        {
            FillFolderList(subdir, list, current_level + 1, depth, width);
            current_width++;
            if (current_width > width) break;
        }
    }
    catch (Exception)
    {
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question