除了调用其他方法,方法还能调用自己,这称为递归

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleApp2
 9 {
10     class Program
11     {
12         /// <summary>
13         /// 递归 本身方法调用自己
14         /// 本例子是查询文件夹下一共有多少个文件
15         /// </summary>
16         /// <param name="args"></param>
17         static void Main(string[] args)
18         {
19             string pash = "D:\\C#教学\\123-C#图解视频教程";
20             ReadDir(pash);
21             Console.ReadLine();
22         }
23         private static void ReadDir(string pash)
24         {
25             //读取文件
26             string[] filsh = Directory.GetFiles(pash);
27             for (int i = 0; i < filsh.Length; i++)
28             {
29                 Console.WriteLine(filsh[i]);
30             }
31             //读取
32             string[] dirs = Directory.GetDirectories(pash);
33             for (int i = 0; i < dirs.Length; i++)
34             {
35                 Console.WriteLine(dirs[i]);
36                 ReadDir(dirs[i]); // 递归
37             }
38         }
39     }
40 }

 

posted on 2020-10-31 13:02  江湖小虾米L  阅读(420)  评论(0编辑  收藏  举报