Luca's
Luca's

Reputation: 373

Save File with AppendText

i have a richTextbox es:

line1
line2
line3

use this for save file File.WriteAllText(name, (richTextBox1.Text)); but the file returns this line1lin2line3 is possiible to save file in this way?

line1
line2
line3

Upvotes: 0

Views: 162

Answers (4)

jagbandhuster
jagbandhuster

Reputation: 707

You can fetch the individual lines by using the 'Lines' property.

string[] linesintextbox = richTextBox1.Lines;
File.WriteAllLines("xyz.txt", linesintextbox);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

I suspect that for whatever reason, RichTextBox is using the Unix linebreak of just \n. Try this:

File.WriteAllText(name, richTextBox1.Text.Replace("\n", "\r\n"));

Upvotes: 3

Samich
Samich

Reputation: 30095

For writing to file:

File.WriteAllLines("my file.txt", this.richTextBox1.Lines);

for appending:

File.AppendAllLines("my file.txt", this.richTextBox1.Lines);

Upvotes: 1

Marco
Marco

Reputation: 57573

What about richTextBox1.SaveFile(file_name)?
Or richTextBox1.SaveFile(file_name, RichTextBoxStreamType.PlainText)

Upvotes: 0

Related Questions