DEV Community

Clint
Clint

Posted on • Updated on

Text Reader

Hi all I'm doing a c# course just got a problem at the moment. can someone please tell me why this isn't reading each line or confirm that I have all the code right for a text reader.

    private void tSBtnOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFile = new OpenFileDialog();
        StreamReader myStreamReader = StreamReader.Null;

        openFile.InitialDirectory = "";

        if (openFile.ShowDialog() == DialogResult.OK) {
            myStreamReader = new StreamReader(openFile.FileName);

            string line = "";

            while (line != null)
            {
                line = myStreamReader.ReadLine();
                if (line != null)
                    textBox1.Text += line;

            }

            myStreamReader.Close();

        }

    }
}

Top comments (3)

Collapse
 
katnel20 profile image
Katie Nelson

Are you just seeing the last line from the file?
Looks like you are overwriting every line in the TextBox. Try changing textBox1.Text = line; to textBox1.Text += line;
This will concatenate the lines into the TextBox.

Collapse
 
clintsoftware profile image
Clint

thank you yes that adds on now but now its all in one line tried char(10), char(13), IsInputChar(10) and IsInputChar(13) but that's not working for a new line.

Collapse
 
clintsoftware profile image
Clint

seems it has tobe (char)10 but still not a new line