bala3569
bala3569

Reputation: 11010

Writing into a text file line by line

I am writing a text into a file. I am getting a column from database and stored it in a string and writing it in a text file. That column contains c# code and it is writing like a single line with small squares for next line(space). I need to write it line by line. Here is my code.

using (var dest = File.AppendText(Path.Combine(_logFolderPath, "a.txt")))
{
    dest.WriteLine(line.TrimStart());
}  

Any suggestion?

Upvotes: 2

Views: 11218

Answers (3)

bala3569
bala3569

Reputation: 11010

dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));

Upvotes: 1

Dean Chalk
Dean Chalk

Reputation: 20481

try this

File.AppendAllText(Path.Combine(_logFolderPath, "a.txt", 
    line.Trim() + Environment.NewLine));

Upvotes: 1

Tim Robinson
Tim Robinson

Reputation: 54774

Does Notepad show small squares for new lines, but when you look at the file in Visual Studio it's OK? If so, my guess is that this will fix it:

dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine));

Upvotes: 4

Related Questions