要获取指定文件夹下的所有子文件夹名称,但不包括子文件夹的内容,你可以使用 C# 的 Directory.GetDirectories()
方法。以下是一个示例代码:
csharpusing System.IO;
// 指定文件夹路径
string folderPath = @"C:\YourFolder";
// 获取文件夹下所有的子文件夹路径
string[] subFolderPaths = Directory.GetDirectories(folderPath);
// 遍历子文件夹路径并打印文件夹名称
foreach (string subFolderPath in subFolderPaths)
{
string folderName = Path.GetFileName(subFolderPath);
Console.WriteLine(folderName);
}
在上面的代码中,Directory.GetDirectories()
方法会返回指定文件夹下所有子文件夹的路径。然后,使用 Path.GetFileName()
函数获取每个子文件夹的名称,并将其打印出来。这样就可以得到指定文件夹下的所有子文件夹名称,但不包括子文件夹的内容。请确保在使用该代码时将 folderPath
替换为你实际的文件夹路径。