10

我有一些代码:

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

我希望显示:documents\iracing\setups\的子文件夹,而不是文件...包括 .sto 文件。我需要的只是列出子文件夹....我不知道该怎么做?谢谢!

4

3 回答 3

21

你可以试试这个:

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

foreach(DirectoryInfo folder in directories)
     listBox2.Items.Add(folder.Name);
于 2012-05-19T20:11:04.467 回答
2

只需使用此功能

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);
}
于 2012-05-19T20:16:03.823 回答
1

如果您希望获得...目录,请使用EnumerateDirectoriesorGetDirectories代替。GetFiles

于 2012-05-19T20:07:42.583 回答