MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 2: OpenFileDialog

2009-04-16 21:23:50 | 731 views | csharp openfiledialog open file dialog input output

The Basics

Sometimes you want to change textfiles in a program. This tutorial will create a program that opens a textfile and let you alter it.
It will look like this:

A user will be able to import a textfile and alter it.

A little list of the three controls involved:

Getting into it

This is what the code looks like:

using System;
using System.Windows.Forms;
using System.IO;

namespace InputOutput

public partial class MainForm : Form

//We'll use this path later on to hold the current path of the file
public string ourPath;

public MainForm()

void CmdBrowseClick(object sender, EventArgs e)
//Create a OpenFileDialog instance to use
OpenFileDialog openDialog = new OpenFileDialog();
//We select what types are permitted to open
openDialog.Filter = "Text Files (*.txt)|*.txt";
//A title for the dialogbox
openDialog.Title = "Open textfile";

//If the user press cancel then end this void
if (openDialog.ShowDialog() == DialogResult.Cancel) {

//Create a filestream
FileStream fStr;
try {
//Set filestream to the result of the pick of the user
fStr = new FileStream(openDialog.FileName, FileMode.Open, FileAccess.Read);

//Create a streamreader, sr, to read the file
StreamReader sr = new StreamReader(fStr);
//While the end of the file has not been reached...
while (sr.Peek() >= 0) {
//Create a 'line' that contains the current line of the textfile
string line = sr.ReadLine();
//Enter the text into the main textbox together with a new line
txtMain.Text += line + Environment.NewLine;
//Close the file so other modules can access it

//Set public variable ourPath to the current path
ourPath = Path.GetDirectoryName(openDialog.FileName);
ourPath += Path.GetFileName(openDialog.FileName);

//If something goes wrong, tell the user
} catch (Exception) {
MessageBox.Show("Error opening file", "Error message");

void CmdAlterFileClick(object sender, EventArgs e)
//Open a streamwriter using public string ourPath
StreamWriter sw = new StreamWriter(ourPath);

//Write the textbox values into the textfile

//Close the textfile

Allright, so the important things here are:

Line 12: We create a public variable where we hold the path to the file we've opened. The string variable gets data from line 52-53.

Line 37: We create a filestream that handles the selected file. It opens a read the file so that we can enter the data into the textbox.

lines 40-49: We open the filestream with a streamreader and read the file into the textbox.

lines 61-70: We re-write the textfile to what's in the textbox.

So, what we've done here is created a program that can open and edit textfiles, sort of like a basic notepad.


These examples shows you how to open a filestream for reading, writing and creating files:

Open an existing file for read and write:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open);

Open existing file for reading:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read);

Open existing file for writing:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Write);

Open file for writing(appending) and, if the file doesn't exist, creates it:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Append);

Create a new file and open it for read and write and if the file exists, overwrites it:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Append);

Create a new file and open it for read and write. Throws an exception if the file exists:

FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.CreateNew);

Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.