SmartDeveloper
SmartDeveloper

Reputation: 58

StreamReader in textbox

I am trying to use StreamReader to read a text file and display the results in a textbox, but everytime I try to do that, my textbox ends up freezing. The same result in Console seems to work fine. Here is my code so far.

private void ReadFile_Click(object sender, EventArgs e)
    {
     string line = "";
     bool IsActive = true;
        try
        {
            using (StreamReader file = new StreamReader("C:\\test.txt"))
            {
                while (!file.EndOfStream)
                {
                    line = file.ReadLine();
                    textBox1.Text = file.ReadLine();
              }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "File not found");
            return;
        }
    }

Upvotes: 1

Views: 4534

Answers (4)

TimeGriffin
TimeGriffin

Reputation: 11

This post seems pretty old, so not sure if you got it working, but I used something similar in some of my code and I got it to work using

textbox.AppendText(stringName)

Upvotes: 1

John Wu
John Wu

Reputation: 52290

Reading it faster

Try reading the whole file at once instead of one line at a time. All those individual updates to the textbox to add a line will get very slow.

If the file is extremely big, you could also use ReadToEndAsync, although then you'd have to figure out what to do with the rest of your UX. Anyway, if the file is that big, you should probably not be using an ordinary textbox.

Note that you may have to do a newline translation from your file to the textbox, depending on the format. I think the textbox delimits multiline with just \n\ whereas your filesystem might use \r\n.

private void ReadFile_Click(object sender, EventArgs e)
{
    try
    {
        using (StreamReader file = new StreamReader("C:\\test.txt"))
        {
            var temp = file.ReadToEnd();
            // temp = temp.Replace("\r\n","\n");  You may or may not have to do something like this 
            textBox1.Text = temp;
        }
    }
    catch (FileNotFoundException ex)
    {
        MessageBox.Show(ex.Message, "File not found");
        return;
    }

Upvotes: 0

John Wu
John Wu

Reputation: 52290

This code is very odd:

while (!file.EndOfStream)
{
    line = file.ReadLine();
    textBox1.Text = file.ReadLine();
}

It seems you are overwriting the textbox over and over. When your code is done running, only the last line will be showing. Also, you are only displaying every other line, since every other other line is stored in the variable line and then discarded without being displayed.

I think you meant

while (!file.EndOfStream)
{
    textBox1.Text += file.ReadLine();
}

or maybe

while (!file.EndOfStream)
{
    textBox1.Text += ("\n" + file.ReadLine());
}

Also, make sure that textBox1 is set to Multiline.

Upvotes: 0

Darren
Darren

Reputation: 70776

You're updating the textbox in the while loop which is most likely locking up the GUI thread (depending on how big your file is), you're also losing some of the original file content.

I would recommend that you build up the file contents using a StringBuilder and then assign the content of this object outside of the loop, which will optimize the performance.

Example:

StringBuilder builder = new StringBuilder();

while (!file.EndOfStream)
{
   builder.Append(file.ReadLine());
}

textBox.Text = builder.ToString();

You could also put the file operation on another Thread or Task and compare performance.

Upvotes: 1

Related Questions