zoukankan      html  css  js  c++  java
  • 遍历电脑下面所有文件--递归

      运行程序,自动遍历我们电脑里面的所有目录和文件,并且写入一个新文件里面,可以全部,也可以指定目录进行操作,递归的运用。

      递归 : 先入后出的原则

      以实例来看, 如下 :

      

     1        static void Main(string[] args)
     2         {
     3 
     6 
     7             StreamWriter sw = new StreamWriter(@"d:ff.txt", false, Encoding.Default);   // 写入新文件
     8 
     9 
    10             System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();              //  获取电脑有几个盘符
    11 
    12             foreach (System.IO.DriveInfo drinfo in drives)
    13             {
    14 
    15                 Console.WriteLine(drinfo.Name.Substring(0, 2));
    16 
    17                 GetDiction(@"" + drinfo.Name.Substring(0, 2) + "", sw);                  //  遍历
    18 
    19             }
    20 
    21             
    22             
    23             Console.ReadKey();
    24 
    25          }
    26 
    27 
    28         public static void GetDiction(string files, StreamWriter sw)
    29         {
    30 
    31 
    32             ArrayList al = new ArrayList();
    33 
    34             ArrayList al2 = new ArrayList();
    35 
    36             DirectoryInfo dirInfo = new DirectoryInfo(files);
    37 
    38 
    39             try
    40             {
    41                 DirectoryInfo[] dir = dirInfo.GetDirectories();    // 获得所有子目录
    42 
    43                 FileInfo[] fis = dirInfo.GetFiles();               // 获得所有文件
    44 
    45 
    46 
    47                 for (int i = 0; i < dir.Length; i++)
    48                 {
    49                     GetDiction(dir[i].FullName, sw);               //  递归 , 注: sw 参数无任何意义,但是可以解决多文件进程问题 .
    50 
    51                     al.Add(dir[i].FullName);
    52                 }
    53 
    54                 
    55 
    56                 for (int i = 0; i < fis.Length; i++)
    57                 {
    58                     al2.Add(fis[i].ToString());
    59                 
    60                 }
    61 
    62  63 
    64                 foreach (var item in al)                            //  遍历  
    65                 {
    66                     Console.WriteLine(item);
    67 
    68                     sw.WriteLine(item);
    69                 }
    70 
    71                 foreach (var item2 in al2)
    72                 {
    73                     Console.WriteLine(item2);
    74 
    75                     sw.WriteLine(item2.ToString());
    76 
    77                 }
    78             }
    79 
    80             catch
    81             { }
    82 
    83 
    84         }
  • 相关阅读:
    const 与 readonly知多少
    js中得~~是什么意思/JS按位非(~)运算符与~~运算符的理解分析
    严格模式详解
    Javascript中的prototype和__proto__的联系区别
    提升页面渲染效率
    window.onload=function(){}和$(function(){})的区别
    gulp的安装和配置
    JS的组成部分、引入页面的方法以及命名规范
    js继承的三种实现
    前端模块化
  • 原文地址:https://www.cnblogs.com/duanshunjie/p/3680612.html
Copyright © 2011-2022 走看看