-1

I have a structure of folders and files which I want to read in and display in a TreeView. Both the folders and files are objects in my C# Visual Studio environment.

A folder object contains:

  • Collection of files
  • Collection of folders

The top level of the structure is a folder. I start here with reading and use a foreach loop to get all the files inside this top level. Then I need to read in the collection of folders to see if the top folder contains other folders. And for every folder I need to do repeat the same process.

I can start at the top level of the structure and check if that folder contains folders but the problem is that I don't know how much layers the structure exists of. Folders can be nested in each other with a unlimited amount of levels. If I use foreach loops I have to nest them in each other but the amount of nested loops will determine how many layers I will read in.

I am looking for a dynamic solution to keep reading the structure untill I reach the last layer without needing to use unnecessary code.

Does anyone has an solution?

PICTURE 1

Image of the structure. All folders and files in the image can be accessed as objects.

PICTURE 2

Image of the code used to loop through the structure.

2 Answers2

1

It's natural to use BFS(https://en.wikipedia.org/wiki/Breadth-first_search) or recursion. BFS goes through the directory hiearchy by layers and there is danger of the stack overflow if you use a recursion version.

example:

static void SearchDir(string dirPath)
{
   Queue<string> queue = new Queue<string>();
   queue.Enqueue(dirPath);

   while(queue.Count() != 0) 
   {
       var actualDir = queue.Dequeue(); 
       foreach(var file in Directory.GetFiles(actualDire)
           //Output info about all files in the directory 

       foreach(var dir in Directory.GetDirectories(actualDir)
       {
           //Output info about all directories in the directory
           queue.Enqueue(dir); 
       }
   }
} 
SuckLips
  • 55
  • 1
  • 11
0

You need to do recursion. Sample as below

static void Main(string[] args)
        {
            DeviceUserGroup folder = GetTopLevelGroupAsYouWish();
            BuildHierarchy (folder);
            Console.ReadKey();
        }

        static void BuildHierarchy(DeviceUserGroup userGroup)
        {
            try
            {
                foreach (Device device in userGroup.Devices)
                    Console.WriteLine(device.Name);
                foreach (DeviceUserGroup group  in userGroup.Groups)
                {
                    Console.WriteLine(group.Name);
                    BuildHierarchy(group);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ansil F
  • 284
  • 1
  • 8
  • The problem is that the structure is not accessible through a file path. The objects are retrieved from an API which is connected to an external software program. This gives me a folder object of the highest layer. I must read out this folder locally in Visual Studio so I can't use recursion. I've added another picture to show you the process that i do. – Max Jeltes Oct 31 '17 at 09:21
  • changed the code based on picture 2 – Ansil F Oct 31 '17 at 09:40