Opening and Reading a Text File using the OpenFileDialog form in C#.NET
The OpenFileDialog object interacts with the Computer's API (Application Programming Interface) to present available files to the user and retrieves the user's file selection back to the program. This object is part of the System.Windows.Forms library so no additional using reference will be needed.
This dialog can be customized to show the file type, beginning directory, and the title to be displayed on the dialog itself.
When you limit the file type to just the extension .txt as we did in this sample code, only those particular file types will be visible to the user although they do have the option to select All files (*) as well.
Let's review the function below (a button click event in this example) that is used to interact with this OpenFileDialog.
- privatevoid btnOpenTextFile_Click(object sender, EventArgs e) {
- //First, declare a variable to hold the user's file selection.
- String input = string.Empty;
- //Because the OpenFileDialog is an object, we create a new instance by declaring a variable with the data type OpenFileDialog and setting it equal to the new instance.
- OpenFileDialog dialog = newOpenFileDialog();
- //Now we set the file type we want to be available to the user. In this case, text files.
- dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- //Next is the starting directory for the dialog and the title for the dialog box are set.
- dialog.InitialDirectory = "C:";
- dialog.Title = "Select a text file";
- //Once the dialog properties are set, it is ready to present to the user.
- if (dialog.ShowDialog() == DialogResult.OK) strFileName = dialog.FileName;
- if (strFileName == String.Empty) return; //user didn't select a file to opena
- //If the user selected a file, then the DialogResult property value will be "OK" and we will also have the file name and path of that file.
- }
Now we can use the StreamWriter and StreamReader to read/write to the text file

Ken BraybrookPosted Mar 11, 2013, 7:03 PM
Is there a way to force a file closed in order to prevent an exception from being thrown when another attempt is made to access it?
Sanju AgrawalPosted May 19, 2012, 2:57 AM
tell me chat room in C# with help of asp.net
Sanju AgrawalPosted May 19, 2012, 2:32 AM
sir pls tell me how to make own browser in C#.........
Sanju AgrawalPosted May 19, 2012, 2:31 AM
Sir can u tell me chat room in C# with Help of Asp.net.....
Sanju AgrawalPosted May 19, 2012, 2:30 AM
Good Article I like it
Sanju AgrawalPosted May 19, 2012, 2:30 AM
Good Article I like it
Sanju AgrawalPosted May 19, 2012, 2:30 AM
Good Article I like it
Ankur GuptaPosted May 8, 2012, 12:40 PM
Hi,sir in which format this file will be open.
Mahesh ChandPosted May 6, 2012, 10:54 PM
Hi Amani, what file format do you want to open in Word? Keep in mind, Microsoft word can open only a limited format types.
Amani HusseineditedPosted Dec 9, 2011, 3:45 PMEdited Dec 9, 2011, 3:46 PM
Hi How i can display the content of file in Microsoft word ??
R JPosted Sep 6, 2010, 7:09 AM
hi... how can i browse both folders and files at a time. If i use openFileDialog -> It can browse only files If i use openFolderdialog -> It can browse folders only. plz suggest me how to do it or give some sample code.