//獲取所有驅動器 string[] drives = Environment.GetLogicalDrives(); foreach (string driver in drives) { Console.WriteLine(driver); //驅動器信息 DriveInfo di = new DriveInfo(driver); //是否准備好 Console.WriteLine("isRead:" + di.IsReady); //名稱 Console.WriteLine("name:" + di.Name); //驅動器根目錄 DirectoryInfo root = di.RootDirectory; RecursiveAsync(root); Console.WriteLine(); } Console.ReadKey(); //方法體 private async static void RecursiveAsync(DirectoryInfo root) { await Task.Run(() => { try { //得到所有文件 FileInfo[] fis = root.GetFiles("*.*"); if (fis != null && fis.Length > 0) { foreach (FileInfo fi in fis) { //文件名 string name = fi.FullName; name = name.Length > 100 ? name.Substring(0, 100) + "…" : name; Console.WriteLine(name); } } //獲取子目錄 DirectoryInfo[] dis = root.GetDirectories(); //存在子目錄 foreach (DirectoryInfo di in dis) { //遞歸子目錄 RecursiveAsync(di); } } catch (Exception e) { Console.WriteLine(e.Message); } }); }