- //获取所有驱动器
- 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);
- }
- });
- }