Kazankoph
Kazankoph

Reputation: 149

Saving contents of rich textbox to file, perserving multilines

Ok so I'm trying to make a simple text editing program and I got the files to save, but the problem is that all line breaks are removed in the saved file

ie

text as it appears in the text editor

123
456
789

text as it appears in the saved file

123456789

code I'm using
string filename = saveFileDialog1.FileName; File.WriteAllText(filename,richTextBox1.Text);
and
string filename = saveFileDialog1.FileName;
File.AppendAllText(filename, richTextBox1.Text);

These both produce the same result ie no linebreaks

Any ideas on what I'm doing wrong?

Upvotes: 1

Views: 5129

Answers (3)

PixelGamer2
PixelGamer2

Reputation: 1

'Maybe try this also this worked for me.

Dim dialog
dialog = SaveFileDialog1.ShowDialog()
RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)

dialog use your variable.

richtextbox1 type the name of your richtextbox.

SaveFileDialog1 type the name of your savefiledialog

Upvotes: 0

XEENA
XEENA

Reputation: 567

Try this.

 File.WriteAllLines(saveFileDialog.FileName, richTextBox.Lines);

Upvotes: 2

sa_ddam213
sa_ddam213

Reputation: 43636

You should just be able to use File.WriteAllLines with the RichTextBox.Lines property, as File.WriteAllText and File.AppendAllText is going to ignore you line formatting.

Example

string filename = saveFileDialog1.FileName;
File.WriteAllLines(filename, richTextBox1.Lines);

Upvotes: 3

Related Questions