Home » Previous Subjects » Fall 2013 – Subjects » Week 7 — File I/O » Monday Instructions and Notes

Monday Instructions and Notes

Today’s project will be a simple application that lets the user create a text file containing text that the user enters.  This is a very similar concept to the notepad.exe application which is installed on all Windows computers.


  1. Open Visual C# Express and create a Winforms application called “SimpleNotepad”
  2. Add a text box to your form and name it inputBox.  Make it multiline, and make it take up the majority of your user interface.
  3. Add a button and name it saveButton.  Change the text on it to say “Save”
  4. Add a button and name it openButton.  Change the text on it to say “Open”
  5. Double click the saveButton in order to create a click event handler for that button.
  6. Switch back to the design view of the form and double click the openButton to create a click event handler for that button.
  7. In the code view for your form, add a property that is a string named Filename


Once we’ve gotten this far, we’ll need more instructions.  Additionally, we need to know a little bit more about the classes involved in File I/O.

In the System.IO namespace, there is a File class, a StreamReader class, and a StreamWriter class.  We’ll be using those two classes in order to complete this project.

The File class has several methods that will be very useful.

File.CreateText(“somefile.txt”)  —  This creates a text file and returns a StreamWriter object, which we’ll get to in a moment.

File.OpenText(“somefile.txt”) — This opens a text file and returns a StreamReader object, which again, we’ll get to in a moment.

File.Delete(“somefile.txt”) — Deletes the specified file

File.Exists(“somefile.txt”) — Returns a bool.  It’s true if the file exists, and false if it does not.

We will be using all of those methods.

The StreamWriter class is used for writing text to a file.  The StreamReader class is used for reading text.

StreamWriter.Write(“text”) — Writes the text

StreamWriter.Flush() — Tells the StreamWriter to empty its buffer

StreamWriter.Close() — Closes the open file that you’ve been writing to, making the file available for other applications.

StreamReader.ReadLine() — Returns a string for the current line of the text file that you’re reading.


Follow these instructions:

  1. In design mode, double click the form box to create a Form_Load event handler
  2. In the Form_Load event handler, set the Filename property to the following value —>  @”C:\Documents and Settings\ccsstudent\My Documents\simplenotepad.txt”
  3. Double check that you got step 2 wrong.  If you got it wrong, everything will fail, and it will fail badly.
  4. Make your open and save click event handler look like this:


private void openButton_Click(object sender, EventArgs e)
 // Clear out our input area
 inputBox.Text = "";
 if (File.Exists(Filename))
 StreamReader reader = File.OpenText(Filename);
 string currentLine = "";
 while ((currentLine = reader.ReadLine()) != null)
 inputBox.Text = inputBox.Text + currentLine;
private void saveButton_Click(object sender, EventArgs e)
 if (File.Exists(Filename))
StreamWriter writer = File.CreateText(Filename);


You should now be able to create and open a single text file.

Leave a Reply

Your email address will not be published. Required fields are marked *