C# tutorials
21. Back and forward buttons ii
Displaying text files

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.


To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag two Buttons and a RichTextBox to Form 1. button1 is the back button and button2 the forward button. You can rename the buttons if you like. Remember, though, that the names button1 and button2 are used below.

6. Double-click on button1 and write or paste this code where the caret is:

try
{
  string[] files = Directory.GetFiles(path);
  if (richTextBox1.Text == string.Empty)
  {
    i = 0;
    MessageBox.Show("Click on button2 to display the first file in the folder.");
  }
  else if (i == 0)
  {
    MessageBox.Show("You have reached the first file in the folder.");
  }
  else
  {
    i = i - 1;
    StreamReader sr = new StreamReader(files[i]);
    string str = sr.ReadToEnd();
    sr.Close();
    richTextBox1.Text = str;
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

7. Double-click on button2 and write or paste this code where the caret is:

try
{
  string[] files = Directory.GetFiles(path);
  if (richTextBox1.Text == string.Empty)
  {
    i = 0;
    StreamReader sr = new StreamReader(files[0]);
    string str = sr.ReadToEnd();
    sr.Close();
    richTextBox1.Text = str;
  }
  else if (i == files.Length - 1)
  {
    MessageBox.Show("You have reached the last file in the folder.");
  }
  else
  {
    i = i + 1;
    StreamReader sr = new StreamReader(files[i]);
    string str = sr.ReadToEnd();
    sr.Close();
    richTextBox1.Text = str;
  }
}
catch (Exception excMsg)
{
  MessageBox.Show(excMsg.Message.ToString(), "Error");
}

8. Write or paste this code right below using System.Windows.Forms;:

using System.IO;

9. Write or paste this code right above private void button1_Click(object sender, EventArgs e):

int i = 0;
string path = @"D:\Textfiles";

10. Change D:\Textfiles to a folder path that actually exists on your computer. The files in the folder should be .txt files or of some other human-readable file type, like .html or .xml.

11. Press F5 to start debugging the program.

12. Click on button1. A MessageBox with the message Click on button2 to display the first file in the folder should appear.

13. Close the MessageBox and click several times on button2. All the files in the folder should appear one at a time. Some characters may not be rendered correctly, because the encoding isn't set in the C# code. After the last file is reached, a MessageBox with the message You have reached the last file in the folder should appear.

14. Close the MessageBox and click several times on button1. All the files in the folder should appear one at a time in reversed order. After the first file is reached, a MessageBox with the message You have reached the first file in the folder should appear.

15. Delete the text in richTextBox1 and repeat step 10 and 11. You should get the same response as you got the first time you did these steps.

Comments on the first code snippet

Line 3: Get the paths to all files in the selected folder. The path to that folder is set in the fourth code snippet.

Line 4-8: If the RichTextBox is empty, set i to 0 (in case the text in the RichTextBox has been deleted and i has some other value at the moment) and display a message.

Line 9-12: If i is 0 and the first file in the array files (with index 0) is displayed, display a message.

Line 13-20: If the RichTextBox isn't empty and i isn't 0, subtract 1 from i and display the file with that index in the array files.

Line 16: Create a new StreamReader for the file files[i] (the currently selected index in the array files).

Line 17: Set the string str to all text in the file files[i].

Line 18: Close the StreamReader sr. It seems, you must do this before you can do anything with the string str.

Line 19: Set the text in richTextBox1 to the string str, which is the same as all text in the file files[i].

Comments on the second code snippet

Line 3: Get the paths to all files in the selected folder. The path to that folder is set in the fourth code snippet.

Line 4-11: If the RichTextBox is empty, set i to 0 (in case the text in the RichTextBox has been deleted and i has some other value at the moment) and display the first file in the array (with index 0).

Line 7: Create a new StreamReader for the file files[0] (the first index in the array files).

Line 8: Set the string str to all text in the file files[0].

Line 9: Close the StreamReader sr. It seems, you must do this before you can do anything with the string str.

Line 10: Set the text in richTextBox1 to the string str, which is the same as all text in the file files[0].

Line 12-15: If i is the length of the array minus 1 and the last file in the array is displayed, display a message. - 1 in line 12 is needed because array index starts with 0 and Length with 1.

Line 16-23: If the RichTextBox isn't empty and i isn't 0, add 1 to i and display the file with that index in the array.

Line 19: Create a new StreamReader for the file files[i] (the currently selected index in the array files).

Line 20: Set the string str to all text in the file files[i].

Line 21: Close the StreamReader sr. It seems, you must do this before you can do anything with the string str.

Line 22: Set the text in richTextBox1 to the string str, which is the same as all text in the file files[i].

Comment on the third code snippet

A StreamReader needs the namespace System.IO to work.

Comments on the fourth code snippet

Line 1: Set the initial value of the int i to 0.

Line 2: Set the string path to the path of the folder you want to display files from.

Final comments

This program can, obviously, be improved in many ways. Among others, you can add a TextBox to get folder paths from, another TextBox to get what file types are displayed from and a ComboBox to get encoding for the StreamReaders from. I will perhaps write a tutorial teaching how to implement these and other features later. In the meantime, you may want to download the source code to the program MK File Tools 2011, that includes a modified version of the code in this tutorial.


Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.