zoukankan      html  css  js  c++  java
  • c# 递归异步获取本地驱动器下所有文件

    1. //获取所有驱动器  
    2.            string[] drives = Environment.GetLogicalDrives();  
    3.            foreach (string driver in drives)  
    4.            {  
    5.                Console.WriteLine(driver);  
    6.                //驱动器信息  
    7.                DriveInfo di = new DriveInfo(driver);  
    8.                //是否准备好  
    9.                Console.WriteLine("isRead:" + di.IsReady);  
    10.                //名称  
    11.                Console.WriteLine("name:" + di.Name);  
    12.                //驱动器根目录  
    13.                DirectoryInfo root = di.RootDirectory;  
    14.                RecursiveAsync(root);  
    15.                Console.WriteLine();  
    16.            }  
    17.            Console.ReadKey();  

    //方法体

    1. private async static void RecursiveAsync(DirectoryInfo root)  
    2.        {  
    3.            await Task.Run(() =>  
    4.            {  
    5.                try  
    6.                {  
    7.                    //得到所有文件  
    8.                    FileInfo[] fis = root.GetFiles("*.*");  
    9.                    if (fis != null && fis.Length > 0)  
    10.                    {  
    11.                        foreach (FileInfo fi in fis)  
    12.                        {  
    13.                            //文件名  
    14.                            string name = fi.FullName;  
    15.                            name = name.Length > 100 ? name.Substring(0, 100) + "…" : name;  
    16.                            Console.WriteLine(name);  
    17.                        }  
    18.                    }  
    19.                    //获取子目录  
    20.                    DirectoryInfo[] dis = root.GetDirectories();  
    21.                    //存在子目录  
    22.                    foreach (DirectoryInfo di in dis)  
    23.                    {  
    24.                        //递归子目录  
    25.                        RecursiveAsync(di);  
    26.                    }  
    27.                }  
    28.                catch (Exception e)  
    29.                {  
    30.                    Console.WriteLine(e.Message);  
    31.                }  
    32.            });  
    33.        }  
  • 相关阅读:
    NavigationBar隐藏
    (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
    Makefile 中:= ?= += =的差别 和条件运行
    C# 使用WinRar命令压缩和解压缩
    C# 字段、属性、成员变量
    js中推断对象详细类型
    Python学习入门基础教程(learning Python)--3.3.3 Python逻辑关系表达式
    JavaScript类数组对象参考
    Codeforces Round 190 div.2 322C 321A Ciel and Robot
    Android Application plugin
  • 原文地址:https://www.cnblogs.com/web100/p/csharp-get-local-file.html
Copyright © 2011-2022 走看看