You can use File.AppendAllText() as an exact drop-in (but you’ll need to append Environment.NewLine to your text).
Make sure that you delete the file if it exists before your foreach loop, possibly prompting the user for confirmation depending on your requirements.
solved I want to read from and write to a file in C#