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 ne开发者_如何转开发wline characters to show up in the file?
Try
streamWriter.WriteLine(TextBox2.Text.Replace(Environment.NewLine, "<br/>"));
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.
Or you wrap the output in pre ("preformatted") tags:
streamWriter.WriteLine("<pre>" + TextBox2.Text + "</pre>");
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.
精彩评论