title: C# - Detecting encoding in a file, write change to file using the found encoding
body: <p>I wrote a small program for iterating through a lot of files and applying some changes where a certain string match is found, the problem I have is that different files have different encodings. So what I would like to do is check the encoding, then overwrite the file in its original encoding.</p>
  
  <p>What would be the prettiest way of doing that in C# .net 2.0?</p>
  
  <p>My code looks very simple as of now;</p>
  
  <pre><code>String f1 = File.ReadAllText(fileList[i]).ToLower();
  
  if (f1.Contains(oPath))
  {
      f1 = f1.Replace(oPath, nPath);
      File.WriteAllText(fileList[i], f1, Encoding.Unicode);
  }
  </code></pre>
  
  <p>I took a look at <a href="https://stackoverflow.com/questions/3746530/auto-encoding-detect-in-c-closed">Auto encoding detect in C#</a> which made me realize how I could detect encoding, but I am not sure how I could use that information to write in the same encoding.</p>
  
  <p>Would greatly appreciate any help here.</p>