This is because you create a StreamWriter and then use File.WriteAllText . The file is already available using StreamWriter .
File.WriteAllText does just that, writes the entire line that you pass to the file. StreamWriter not required if you intend to use File.WriterAllText .
If you do not want to overwrite the existing file, you can do this:
private void writeTextFile(string filePath, string text) { File.WriteAllText(filePath, text); }
If you want to use StreamWriter (which, by the way, File.WriteAllText uses, it just hides it) and adds to the file, you can do this (from this answer ):
using(StreamWriter sw = File.AppendText(path)) { tw.WriteLine(text); }
source share