Output all files under the folder to the log file learning example of c recursive algorithm

  • 2020-06-07 05:12:25
  • OfStack

Algorithm articles, always bring us endless thinking and interest, 1 problem, a variety of solutions, see how you think about it, for the title of the problem, I think, using recursion is more effective method, of course, recursion has many USES, such as tree classification list operation and so on.

Note:

When using recursion, the special thing for beginners to pay attention to is "exit", you must provide an exit for recursion, otherwise you will run out of memory.

Recursion in code:


static void GetFiles(List<string> arr, string dir)
        {
            arr.AddRange(Directory.GetFiles(dir));
            var subDir = Directory.GetDirectories(dir).ToList();
            if (subDir != null && subDir.Count > 0)
                subDir.ForEach(j =>
                {
                    GetFiles(arr, j);
                });
        }

Program entrance


static void Main(string[] args)
        {
            string path = "F:\\softmare\\Fiddler2 localization ";
            var obj = FileSync(path);
            using (System.IO.StreamWriter srFile = new System.IO.StreamWriter(path + "\\filelist.txt"))
            {
                obj.ForEach(i =>
                {
                    srFile.WriteLine(i);
                });
            }
            Console.WriteLine(" Fill the complete ");
            Console.ReadKey();
        }


Related articles: