I'm using the writeAlltext method to save a text file with the contents saved in string variable namely textFile1.

File.WriteAllText(saveFileDialog1.FileName, textFile1);

However for each blank line i'm getting two boxes in the output text file. I need to keep the formatting as it is while saving a file. the blank lines are being replaced by the boxes.

Any ideas?

Dani AI

Generated

Brief recap and clarification from the thread: saw control‑character boxes in the saved .txt because the file contained lone LF characters instead of the Windows CR+LF sequence. was right to point at nonprintable characters, and ’s replacement approach fixed the issue for the original poster. The behavior is an artifact of how some Windows editors (historically Notepad) render line breaks when only LF is present.

A more robust approach is to normalize all incoming line endings before writing, rather than assuming one form. Normalization handles inputs that might contain CRLF, CR-only, or LF-only (common when text comes from web sources, Unix tools, or pasted content). The example below shows a compact way to convert any newline style to the environment’s newline marker:

using System.Text.RegularExpressions;

string NormalizeNewlines(string s) =>
    Regex.Replace(s, @"\r\n?|\n", Environment.NewLine);

Two practical ways to write the normalized text without worrying about control characters: (1) call the normalization function and save with a single-file write method, or (2) split into logical lines and write them with APIs that append the platform newline automatically (for example, File.WriteAllLines or a StreamWriter and WriteLine). Both avoid hardcoding literal CR/LF sequences and are resilient across platforms.

Quick troubleshooting tips not covered in the replies: inspect the raw characters (use a hex or “Show all characters” view in an editor like Notepad++ or VS Code) to confirm whether CR (13) or LF (10) is present; print character codes in a small loop to identify unexpected control characters; and prefer Environment.NewLine or framework WriteLine methods so output matches the running environment. This closes the gap between the question and the short fix offered by , and explains why the boxes appeared in the first place.

Recommended Answers

All 10 Replies

What do you mean by boxes? Any nonprintable character is most often printed as a "box" character. I suspect linefeed and carriage return characters here.(ASCII 10 and 13, ASCII 65 is the 'A' character)

It's the line feed i guess i've to deal with. But how? How can I have a check on the ascii and even if i get's the line feed ascii how will I be able to represent it with a line feed in text file instead of nonprintable box char.

As I said, i'm using the File.WriteAllText method.

thx

What do you mean by boxes? Any nonprintable character is most often printed as a "box" character. I suspect linefeed and carriage return characters here.(ASCII 10 and 13, ASCII 65 is the 'A' character)

Change to multiline text boxes?

Please find the attached file to see the output text. There should be a line gap or a start of a new paragraph which is not the case. Rather the box char is being saved instead of a line feed while saving the contents to .txt file using File.WriteAllText method.

Change to multiline text boxes?

I see no boxes or empty lines when I open Output.txt with NotePad.

Very common mistake for who works with files, that they don't replace \n with \r\n
your text in textbox.Text = "lmlmalma\n lmlmla" that's wrong to do that File.WriteAllText(saveFileDialog1.FileName, textFile1); rather File.WriteAllText(saveFileDialog1.FileName, textFile1.Replace("\n", "\r\n"));

Hm...strange

Can you see the difference b/w the two. The Personal Loan one is what I want and the output one is what i'm actually getting.

I hope you can notice the line spaces in the first one which i'm unable to produce in the output one.

I see no boxes or empty lines when I open Output.txt with NotePad.

Did you check my reply?

Got it. Thanks man, it solved the problem :)
thx

Very common mistake for who works with files, that they don't replace \n with \r\n
your text in textbox.Text = "lmlmalma\n lmlmla" that's wrong to do that File.WriteAllText(saveFileDialog1.FileName, textFile1); rather File.WriteAllText(saveFileDialog1.FileName, textFile1.Replace("\n", "\r\n"));

You're more than welcome, please don't forget to mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.