Amra
Amra

Reputation: 211

How To Make a "Enter" key by C# in ASP.net

I wrote this code in C#:

using (StreamWriter streamWriter = File.CreateText(@"C:\File.Html"))
{
    streamWriter.WriteLine(TextBox2.Text);
}

This code opens File.Html and copies the value of TextBox2 But When I open File.Html all characters are on one line, even though there were multple lines of text in the TextBox. How do I get the newline characters to show up in the file?

Upvotes: 0

Views: 1181

Answers (4)

Davorin
Davorin

Reputation: 1278

Try

streamWriter.WriteLine(TextBox2.Text.Replace(Environment.NewLine, "<br/>"));

Upvotes: 3

Peter
Peter

Reputation: 3008

Or you wrap the output in pre ("preformatted") tags:

streamWriter.WriteLine("<pre>" + TextBox2.Text + "</pre>");

Upvotes: 1

Ioannis Karadimas
Ioannis Karadimas

Reputation: 7896

New lines in HTML are ignored by default. This is why you are not seeing the results you expect to. In order to insert a newline, you need to replace \n, which stands for newline, with <br />, which is the line break equivalent in HTML.

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66389

You can also use File.WriteAllText instead, avoid having to create new object and dispose it.

You do need to replace new lines with <br /> as suggested above.

Upvotes: 0

Related Questions