title: Iterating multiple txt files in folder to read them in C#
body: <p>Problem: I need to iterate through multiple files in a folder and read them. They are .txt files. While reading I need to note what words occured in each file.</p>
  
  <p>For example:</p>
  
  <p>File 1 text: "John is my friend friend" -> words: John, is, my, friend</p>
  
  <p>File 2 text: "John is Mark" -> words: John, is, Mark</p>
  
  <p>Currently I was reading files and then making it one big file, but it does not work like this so I have to read them separately. Old idea:</p>
  
  <pre><code>string[] filesZ = { "1.txt", "2.txt" };
  
  var allLinesZ = filesZ.SelectMany(i =&gt; System.IO.File.ReadAllLines(i));
  System.IO.File.WriteAllLines("n.txt", allLinesZ.ToArray());
  
  var logFileZ = File.ReadAllLines("n.txt");
  </code></pre>
  
  <p>So this is the first question, how to iterate through them and reading all of them without making a big file.</p>
  
  <p>The second one will be how to make a counter to all of the words for seperate files, currently for one big file I am using:</p>
  
  <pre><code>var logFileZ = File.ReadAllLines("n.txt");
  
  List&lt;string&gt; LogListZ = new List&lt;string&gt;(logFileZ);
  
  var fi = new Dictionary&lt;string, int&gt;();
  LogListZ.ForEach(str =&gt; AddToDictionary(fi, str));
  
  foreach (var entry in fi)
  {
      Console.WriteLine(entry.Key + ": " + entry.Value);
  }
  </code></pre>
  
  <p>This is AddToDictionary:</p>
  
  <pre><code>static void AddToDictionary(Dictionary&lt;string, int&gt; dictionary, string input)
  {
      input.Split(new[] { ' ', ',', '.', '?', '!', '.' }, StringSplitOptions.RemoveEmptyEntries).ToList().ForEach(n =&gt;
      {
          if (dictionary.ContainsKey(n))
              dictionary[n]++;
          else
              dictionary.Add(n, 1);
      });
  }
  </code></pre>
  
  <p>I was thinking about making a loop through all the files (is it possible?) and inside make a counter that counts word for example John in how many files it was. I don't need a specific file number, just a number of occurence of a word, without counting (like in example file 1) words twice (friend).</p>