2

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?

Furqan Safdar
  • 15,658
  • 12
  • 57
  • 89
bala3569
  • 10,470
  • 28
  • 96
  • 143

3 Answers3

4

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));
Tim Robinson
  • 51,912
  • 9
  • 117
  • 136
  • +1 - Though since the dest.WriteLine method appends a line feed character of its own, you might want to Replace the "\n" characters with string.Empty. – sheikhjabootie Nov 30 '10 at 10:43
1

try this

File.AppendAllText(Path.Combine(_logFolderPath, "a.txt", 
    line.Trim() + Environment.NewLine));
Dean Chalk
  • 19,361
  • 6
  • 58
  • 86
1
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));
bala3569
  • 10,470
  • 28
  • 96
  • 143