0
votes

I'm sorry for asking noobish questions, but I am one :).

I can write a .txt file using Write or WriteLine, which reads the whole TextBox. The problem is when I read it. I can not read it using ReadLine. It gives the whole text on one line. It must be a problem with the reading, because in NotePad, I get the file correctly.

What is the reason of this quite strange behavior, and how can I change it? method containing StreamReader

StreamReader streamreader = new StreamReader(openfiledialog.FileName);

textbox.Text = "";

while (!streamreader.EndOfStream)
{
    string read_line = streamreader.ReadLine();
    textbox.Text += read_line + "\n";
}

streamreader.Close();

method containing StreamWriter

StreamWriter streamwriter = new StreamWriter(savefiledialog.FileName);

streamwriter.Write(textbox.Text);

streamwriter.Close();

Thanks in advance.

UPDATED: ReadToEnd worked

2
you need to show your code.... - Keith Nicholas
show your code, its hard to understand what you did wrong - Renatas M.

2 Answers

0
votes

Without seeing any code the best guess I have is you're using different line separators between the textbox and the text file.

I'd guess you either need to format the data to make sure the data gets the right separator for the source, or change the newline separator for the textbox.

0
votes

Couple of possibilities here.

  1. The text in the File is not UTF-8, so it needs to be converted to UTF-8 and then assigned to the text box.
  2. The Textbox has a character limit that needs to be increased
  3. Width of Text Box. Wrapping of text could make a difference.

Usually you would use ReadToEnd if you want the whole file worth of text in one run and ReadLine if you want 1 line. Difference here is in the encoding of the file. 1 Line in a text editor could be different from another. Some Text Editor convert the text to other encodings and some do not, before displaying. Would recommed Notepad++, because it will tell you at the bottom what encoding the file is in and let you change the encoding and save the file for testing.

.net is based on UTF-8 encoding for strings, so a difference in ecoding of text could make a big difference.

Best of Luck