I have this code:
string directory;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
directory = fbd.SelectedPath;
txtSource.Text = directory;
DirectoryInfo d = new DirectoryInfo(directory);
FileInfo[] Files = d.GetFiles();
List<String> str = new List<string>();
foreach (FileInfo file in Files)
{
str.Add(file.Name);
}
}
I have a FolderBrowseDialog
where I select the Path of the folder.
In this selected folder are 3 other folders. I want to read out the names of these folders. I dont want to know or read out the names of files.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.
You can use Directory.GetDirectories()
:
string[] subdirs = Directory.GetDirectories(fbd.SelectedPath);
This gives you the full paths to the subdirectories. If you only need the names of the subfolders, but not the full path, you can use Path.GetFileName()
:
string[] subdirs = Directory.GetDirectories(fbd.SelectedPath)
.Select(Path.GetFileName)
.ToArray();
Or if you want both:
var subdirs = Directory.GetDirectories(fbd.SelectedPath)
.Select(p => new {
Path = p,
Name = Path.GetFileName(p)})
.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With