c# 遍历D盘下的所有文件夹路径(包括子文件夹)并一条条输出

输出 文件夹路径地址
2024-11-26 04:19:58
推荐回答(1个)
回答1:

static void Main(string[] args)
{
string root = @"D:\";
DirectoryInfo info = new DirectoryInfo(root);
ViewDirectory(info);
Console.ReadKey();
}
static void ViewDirectory(DirectoryInfo info)
{

FileInfo[] files = info.GetFiles();
//show all files
if (files != null
&& files.Length > 0)
foreach (FileInfo file in files)
{
Console.WriteLine(file.FullName);
}
DirectoryInfo[] directoies = info.GetDirectories();
//show all sub directories
if (directoies != null
&& directoies.Length > 0)
{
foreach (DirectoryInfo d in directoies)
{
Console.WriteLine(d.FullName);
ViewDirectory(d);
}
}
}