Reading from and Writing to Text Files in C# WinForms

Use StreamReader and StreamWriter in your WinForms apps

Want to let users save their input, or load data from a file into your form? With just a few lines of code, you can store and retrieve text using file streams.

In C#, we use StreamWriter to write to files and StreamReader to read from them โ€” safely and efficiently.


๐Ÿ›  Required Namespace

At the top of your form:

using System.IO;

๐Ÿ–Š Writing to a Text File

โœ๏ธ Example: Save Form Data

private void btnSave_Click(object sender, EventArgs e)
{
    string name = txtName.Text;
    string age = txtAge.Text;

    using (StreamWriter writer = new StreamWriter("user_data.txt"))
    {
        writer.WriteLine("Name: " + name);
        writer.WriteLine("Age: " + age);
    }

    MessageBox.Show("Data saved to user_data.txt");
}

โœ… using block ensures the file is closed properly
โœ… Creates the file if it doesn’t exist, overwrites it if it does


๐Ÿ“– Reading from a Text File

๐Ÿ“‚ Example: Load File into a TextBox

private void btnLoad_Click(object sender, EventArgs e)
{
    if (File.Exists("user_data.txt"))
    {
        using (StreamReader reader = new StreamReader("user_data.txt"))
        {
            txtOutput.Text = reader.ReadToEnd();
        }
    }
    else
    {
        MessageBox.Show("File not found.");
    }
}

โœ… ReadToEnd() grabs the whole file as a string
โœ… Also possible: ReadLine() to read line-by-line


โœจ Example: Append to a File

Instead of overwriting:

using (StreamWriter writer = new StreamWriter("log.txt", append: true))
{
    writer.WriteLine(DateTime.Now + ": User clicked Save");
}

๐Ÿ“š Example Use Case: Save and Load Notes

UI Elements:

  • TextBox txtNotes (Multiline)
  • Button btnSaveNotes
  • Button btnLoadNotes

Code:

private void btnSaveNotes_Click(object sender, EventArgs e)
{
    using (StreamWriter sw = new StreamWriter("notes.txt"))
    {
        sw.Write(txtNotes.Text);
    }

    MessageBox.Show("Notes saved!");
}

private void btnLoadNotes_Click(object sender, EventArgs e)
{
    if (File.Exists("notes.txt"))
    {
        using (StreamReader sr = new StreamReader("notes.txt"))
        {
            txtNotes.Text = sr.ReadToEnd();
        }
    }
    else
    {
        MessageBox.Show("No notes found.");
    }
}

๐Ÿงช Quick Challenge

๐Ÿงฉ Create a form that:

  • Lets users enter Name and Feedback
  • Saves the input to a file using StreamWriter
  • Loads the content back into a multiline TextBox using StreamReader

๐Ÿ“‹ Summary

ClassUse Case
StreamWriterSave (write) data to a file
StreamReaderLoad (read) data from a file
using blockEnsures file is closed safely
File.Exists()Check before reading

โœ… Best Practices

  • โœ… Use using blocks to manage file streams safely
  • โœ… Check for File.Exists() before reading
  • โœ… Catch exceptions (try/catch) for file access errors
  • โœ… Use relative paths for local files ("data.txt")
  • โœ… Consider file dialogs (OpenFileDialog, SaveFileDialog) for flexible paths

๐ŸŽ“ Want to Go Further?

  • Save structured data as CSV or JSON
  • Load and parse configuration files on form start
  • Use File.AppendText() for logging
  • Add save/load confirmation dialogs

๐Ÿ’ฌ Want help wiring up file saving for your forms or reports?
Just share your UI setup โ€” Iโ€™ll help you make it persistent!