Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.WriteAllText Doesn't Preserve Newline

Tags:

c#

.net

The Output of this code

string fileDateTime = "StepsGA-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".txt";
string fname = System.IO.Path.Combine(System.Environment.CurrentDirectory, fileDateTime);
File.WriteAllText(fname, txtSteps.Text);

doesn't preserve newline as it should be. All the contents of the textbox are printed in a horizontal long line ! Where have I gone wrong? Thanks !

like image 637
Jishan Avatar asked Jan 02 '13 07:01

Jishan


People also ask

What does writealltext do in Python?

WriteAllText (String, String) Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

How do you preserve newlines in HTML?

Preserve Newlines, Line Breaks, and Whitespace in HTML Use the white-space: pre; setting to display long-form content containing whitespace and line breaks the same way as provided by a user. The text will also stay inside the container boundaries and now overflow the parent: <span style="white-space: pre;"> This is a long-form text.

What is the default encoding of the writealllines method?

The default behavior of the WriteAllLines (String, IEnumerable<String>) method is to write out data by using UTF-8 encoding without a byte order mark (BOM).

How do you preserve line breaks in HTML?

Preserve Newlines, Line Breaks, and Whitespace in HTML Sometimes you want to display the content in the same format as typed in a textarea. For example, a text from a textarea may contain line breaks or empty lines. The white-space CSS property is helpful displaying the text in the same way it’s provided by the user.


1 Answers

Got the answer ! Instead of \n I needed to use Environment.NewLine.

like image 157
Jishan Avatar answered Oct 06 '22 14:10

Jishan