-1

I'm new to C# and I have a problem. I would like to have the content of a file written to a RichTextBox, but the StreamReader.ReadLine method reads only the first line.

How can I solve this?

Kit
  • 20,354
  • 4
  • 60
  • 103
Jonas
  • 3
  • 1
  • 3
    Call it more than once..? Or just use the `ReadToEnd` method? – Rufus L Jan 31 '19 at 20:16
  • Sorry, Google Translator... ._. – Jonas Jan 31 '19 at 20:19
  • 1
    What is the problem with [RichTextBox.LoadFile](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.richtextbox.loadfile?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Forms_RichTextBox_LoadFile_System_String_)? – Steve Jan 31 '19 at 20:19
  • Possible duplicate of [What is the difference between File.ReadLines() and File.ReadAllLines()?](https://stackoverflow.com/questions/21969851/what-is-the-difference-between-file-readlines-and-file-readalllines) – mjwills Jan 31 '19 at 20:26
  • How do you know how many lines to read from the file? Are you reading a fixed number of lines or reading until a terminating character, or reading the entire file? – jdweng Jan 31 '19 at 20:34
  • I'd like to read every line. @jdweng – Jonas Jan 31 '19 at 20:43
  • And every line goes into the RichTextBox? – jdweng Jan 31 '19 at 20:48
  • Yes, that's right. – Jonas Jan 31 '19 at 21:04

2 Answers2

1

Probably the easiest way to do this would be to use the System.IO.File class's ReadAllText method:

myRichTextBox.Text = File.ReadAllText(filePath);

This class has a bunch of static methods that wrap the StreamReader class for you that make reading and writing to files quite easy.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

There are several ways to read a file. If you want to it with a StreamReader and want to read the whole file, this could be a solution:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

You could edit the part were the output of the console happens. Here it would be possible to concatenate the string for your RichTextbox with

text += Environment.NewLine + line;
Daniel Schneider
  • 63
  • 1
  • 1
  • 7
  • Note that `text = text + line;` will remove all the newline characters from the file contents. You might instead do `text += Environment.NewLine + line;` – Rufus L Jan 31 '19 at 22:19