c# 递归异步获取本地驱动器下所有文件
2013/11/20 23:29:28 web8 博客园
我要评论(0)
- 摘要://获取所有驱动器string[]drives=Environment.GetLogicalDrives();foreach(stringdriverindrives){Console.WriteLine(driver);//驱动器信息DriveInfodi=newDriveInfo(driver);//是否准备好Console.WriteLine("isRead:"+di.IsReady);//名称Console.WriteLine("name:"+di.Name)
- 标签:C# 文件 递归 异步
class="dp-c" start="1">
-
- 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);
- }
- });
- }