Listing Only SubFolders In C#?

风格不统一 提交于 2019-12-01 01:00:38

问题


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!


回答1:


You can try this:

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

foreach(DirectoryInfo folder in directories)
     listBox2.Items.Add(folder.Name);



回答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);
}



回答3:


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



来源:https://stackoverflow.com/questions/10668481/listing-only-subfolders-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!