Algorithm ~ output All files under the folder to the log file (including all subfolders)

Source: Internet
Author: User

Concept:

Algorithm article, always bring us endless thinking and interest, a problem, a variety of solutions, see how you think about it, for the title of the problem, I think, the use of recursion is more effective method, of course, recursive return there are many use occasions, such as tree-type classification list of the operation and so on.

Attention:

When using recursion, the beginner should pay special attention to the "exit", must provide an exit for recursion, otherwise your memory will overflow, hehe, memories overflow everyone must have seen, is from then came, hehe.

Recursion in the code:

Core 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 entry

  static void Main (string[] args)        {            string path = "F:\\softmare\\fiddler2";            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 Complete");            Console.readkey ();        }

Results

Let the program code become more artistic!

Algorithm ~ output All files under the folder to the log file (including all subfolders)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.