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.        }  
  • 相关阅读:
    进程通信方式-管道pipe
    进程间通信
    信号的发送与处理
    信号应用于事件通知
    信号的屏蔽,信号集
    信号的发送kill,raise,alarm,setitimer,abort,sigqueue
    信号处理函数的返回sigsetjmp/siglongjmp
    POJ 1562 Oil Deposits
    HDU 1016 Prime Ring Problem
    HDU 1010 Tempter of the Bone
  • 原文地址:https://www.cnblogs.com/web100/p/csharp-get-local-file.html
Copyright © 2011-2022 走看看