10

I have some code:

string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, @"documents\iracing\setups\");
DirectoryInfo dinfo = new DirectoryInfo(pathDownload); // Populates field with all Sub Folders
FileInfo[] Files = dinfo.GetFiles("*.sto");
foreach (FileInfo file in Files)
{
     listBox2.Items.Add(file.Name);
}

I want the subFolders of: documents\iracing\setups\ to be shown, not the files...including the .sto files. All i need is to list the Subfolders....i have no idea how to do that? Thanks!

3 Answers 3

22

You can try this:

DirectoryInfo directory = new DirectoryInfo(pathDownload);
DirectoryInfo[] directories = directory.GetDirectories();

foreach(DirectoryInfo folder in directories)
     listBox2.Items.Add(folder.Name);
1
  • how would i do it to populate my listbox2 with these subfolders? May 19, 2012 at 20:12
2

Just use this function

string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, @"documents\iracing\setups\");
DirectoryInfo dinfo = new DirectoryInfo(pathUser); // Populates field with all Sub Folders
DirectoryInfo[] directorys = dinfo.GetDirectories();
foreach (DirectoryInfo directory in directorys)
{
    listBox2.Items.Add(directory.Name);
}
0
1

Use EnumerateDirectories or GetDirectories instead of GetFiles if you wish to get... directories.

2
  • So, what do i put inside the ()'s? : FileInfo[] Files = dinfo.GetDirectories(); May 19, 2012 at 20:09
  • @EliteGamer - Did you read the links? You really need to learn to read documentation. The return type and needed parameters (if any) are listed.
    – Oded
    May 19, 2012 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.