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)