title: How to write list<String> to a text file?
body: <p>How can append f to a list each time, then write the list into a text file ? I tried with the following code but the text file is always empty?</p>
  
  <pre><code>  static void DirSearch(string dir, string pattern)
      {
          try
          {
              List&lt;String&gt; filesList = new List&lt;string&gt;();
  
              foreach (string f in Directory.GetFiles(dir, pattern))
              //Console.WriteLine(f);
              {
                  // Console.WriteLine(f); // // &lt;- This works correctly
                  filesList.Add(f);
              }
              foreach (string d in Directory.GetDirectories(dir))
              {
                  //Console.WriteLine(d); 
                  DirSearch(d, pattern);
              }
  
              File.WriteAllLines("files.txt", filesList.ToArray());
  
          }
          catch (System.Exception ex)
          {
              //Console.WriteLine(ex.Message);
          }
      }
  </code></pre>