[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);
}
Re: Get names of subfolders
System.Io.Path.GetDirectoryName()
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 :)
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
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));
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);
}
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.