|
-
Feb 7th, 2008, 03:04 PM
#1
Thread Starter
Hyperactive Member
[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);
}
-
Feb 7th, 2008, 03:14 PM
#2
Re: Get names of subfolders
System.Io.Path.GetDirectoryName()
-
Feb 7th, 2008, 03:16 PM
#3
Re: Get names of subfolders
C# Code:
string[] folders = Directory.GetDirectories(Application.StartupPath);
foreach(string folder in folders)
{
this.listBox1.Items.Add(System.IO.Path.GetDirectoryName(folder));
}
You'd use the System.IO.Path.GetDirectoryName function
-
Feb 7th, 2008, 03:26 PM
#4
Thread Starter
Hyperactive Member
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
-
Feb 7th, 2008, 03:31 PM
#5
Re: Get names of subfolders
Ah.
Seems like using GetFileName instead of GetDirectoryName actually works:
vb Code:
this.listBox1.Items.Add(System.IO.Path.GetFileName(folder));
-
Feb 7th, 2008, 03:36 PM
#6
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);
}
-
Feb 7th, 2008, 05:08 PM
#7
Re: [RESOLVED] Get names of subfolders
CSharp Code:
this.listBox1.DisplayMember = "Name"; this.listBox1.ValueMember = "FullName"; 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|