Results 1 to 7 of 7

Thread: [RESOLVED] Get names of subfolders

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [RESOLVED] Get names of subfolders

    Hi,
    Need to get names of all subfolders within the Application folder.

    Currently I'm getting the full paths with the following code, but I'd like to get just the folder names.
    Code:
    string[] folders = Directory.GetDirectories(Application.StartupPath);
    foreach(string folder in folders)
    {
        this.listBox1.Items.Add(folder);
    }
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Get names of subfolders

    System.Io.Path.GetDirectoryName()

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Get names of subfolders

    C# Code:
    1. string[] folders = Directory.GetDirectories(Application.StartupPath);
    2. foreach(string folder in folders)
    3. {
    4.     this.listBox1.Items.Add(System.IO.Path.GetDirectoryName(folder));
    5. }

    You'd use the System.IO.Path.GetDirectoryName function
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Get names of subfolders

    Thanks guys but this still returns the full paths:
    C:\test\folderA
    C:\test\folderB

    And I need:
    \folderA
    \folderB
    Thanks

    Tomexx.

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Get names of subfolders

    Ah.
    Seems like using GetFileName instead of GetDirectoryName actually works:

    vb Code:
    1. this.listBox1.Items.Add(System.IO.Path.GetFileName(folder));
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Get names of subfolders

    Use directoryinfo then.

    Code:
    string[] folders = Directory.GetDirectories(Application.StartupPath);
    foreach(string folder in folders)
    {
    System.Io.DirectoryInfo di = new System.Io.DirectoryInfo(folder);
    
        this.listBox1.Items.Add(di.Name);
    }

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Get names of subfolders

    CSharp Code:
    1. this.listBox1.DisplayMember = "Name";
    2. this.listBox1.ValueMember = "FullName";
    3. this.listBox1.DataSource = new System.IO.DirectoryInfo(Application.StartupPath).GetFiles();
    That gives you the advantage of being able to get the full path from the SelectedValue if you want. If you don't then you don't need to set the ValueMember.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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