New line character in text file (C#)

Kieran Paddock picture Kieran Paddock · Jan 29, 2016 · Viewed 24.5k times · Source

I'm exporting text to a file in C# using System.IO.File.AppendAllText, and passing in the text file, and then the text I want to export with \n added to the end. When I view the text document, they are not on different lines, although that pesky return-line character is there between the lines. So the system may think it's two line, but a user sees it as one. How can this be fixed automatically without doing a find-replace every time I generate a file?

System.IO.File.AppendAllText(@"./WarningsLog.txt", line + "\n");

Answer

user853710 picture user853710 · Jan 29, 2016

You need to use the Environment.NewLine instead of \n, because newline can be more than that. in windows (if I'm not mistaken), the default is actually \r\n

Although, using \r\n, will help you temporary, using Environment.NewLine is the proper way to go