Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. subdirectories

subdirectories

Scheduled Pinned Locked Moved C#
questionalgorithmsdata-structureshelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pyro Joe
    wrote on last edited by
    #1

    hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!

    C C 2 Replies Last reply
    0
    • P Pyro Joe

      hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You need to write a recursive function, that is, one that 'collects' all the directories within your main one, and calls itself for each, so it keeps drilling down. Christian Graus - Microsoft MVP - C++

      1 Reply Last reply
      0
      • P Pyro Joe

        hey everyone, I've been searching for a way to get an array that includes every directory within a selected directory. say the parent directory was called "hello", I want to get every directory, even the directories within the directories returned. (i.e. hello/hieveryone/helloagain/). There is a "Directory.GetDirectories" method, but this only returns the directories immediately below the selected one. How do I get all the ones below that one, and below that one and send them to an array? all the levels below would be nice, but atleast a minimum of 12 levels below the directory. sry for the wordiness, I didn't know how else to word it. thanks a lot for your help!

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Pyro Joe wrote: How do I get all the ones below that one, and below that one and send them to an array? By doing a recursive call like this:

        public ArrayList Directories(DirectoryInfo startDir)
        {
        ArrayList list = new ArrayList();
        DirectoryInfo[] dirs = startDir.GetDirectories();
        foreach(DirectoryInfo dir in dirs)
        {
        list.Add(dir);
        list.AddRange(Directories(dir));
        }
        }

        See how the Directories() calls itself, but for the subdirectory. Does this help? DISCLAIMER: I typed the code directly in so there may be some syntax errors


        My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

        P 1 Reply Last reply
        0
        • C Colin Angus Mackay

          Pyro Joe wrote: How do I get all the ones below that one, and below that one and send them to an array? By doing a recursive call like this:

          public ArrayList Directories(DirectoryInfo startDir)
          {
          ArrayList list = new ArrayList();
          DirectoryInfo[] dirs = startDir.GetDirectories();
          foreach(DirectoryInfo dir in dirs)
          {
          list.Add(dir);
          list.AddRange(Directories(dir));
          }
          }

          See how the Directories() calls itself, but for the subdirectory. Does this help? DISCLAIMER: I typed the code directly in so there may be some syntax errors


          My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More

          P Offline
          P Offline
          Pyro Joe
          wrote on last edited by
          #4

          yeah, thanks! I was gonna try to use a while loop, but that could get really complex logic-wise. but the foreach thing is really handy. thanks!

          P 1 Reply Last reply
          0
          • P Pyro Joe

            yeah, thanks! I was gonna try to use a while loop, but that could get really complex logic-wise. but the foreach thing is really handy. thanks!

            P Offline
            P Offline
            Pyro Joe
            wrote on last edited by
            #5

            actually, there are two more arguements I just found out about. One that you can tell it to search ALL the subdirectories. but I still thank you for your time in answering my question.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups