在 C# 中只列出子文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10668481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listing Only SubFolders In C#?
提问by Hunter Mitchell
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!
我希望documents\iracing\setups\显示以下子文件夹:而不是文件...包括 .sto 文件。我只需要列出子文件夹....我不知道该怎么做?谢谢!
采纳答案by Omar
You can try this:
你可以试试这个:
DirectoryInfo directory = new DirectoryInfo(pathDownload);
DirectoryInfo[] directories = directory.GetDirectories();
foreach(DirectoryInfo folder in directories)
listBox2.Items.Add(folder.Name);
回答by Oded
Use EnumerateDirectoriesor GetDirectoriesinstead of GetFilesif you wish to get... directories.
如果您希望获得...目录,请使用EnumerateDirectories或GetDirectories代替GetFiles。
回答by Krishanu Dey
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);
}

