在 C# 中仅列出子文件夹
本文关键字:文件夹 | 更新日期: 2023-09-27 18:34:38
我有一些代码:
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 文件。我所需要的只是列出子文件夹....我不知道该怎么做?谢谢!
你可以试试这个:
DirectoryInfo directory = new DirectoryInfo(pathDownload);
DirectoryInfo[] directories = directory.GetDirectories();
foreach(DirectoryInfo folder in directories)
listBox2.Items.Add(folder.Name);
只需使用此函数
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);
}
如果您希望获得,请使用EnumerateDirectories
或GetDirectories
而不是GetFiles
...目录。