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

astoyaPosted Dec 2, 2024, 10:04 PM
If (*variable* is null){ btnSaveAs_Click(sender, e) } else { method(variable) }
Todd HoatsonPosted Oct 7, 2021, 9:48 PM
Very helpful, thank you, Mahesh! One question: "The only purpose of OpenFileDialog to display available colors, create custom colors and select a color from these colors. Once a color is selected, we need that color in our code so we can apply it on other controls." Did you mean to say 'files' instead of 'colors'???
Bob SpijkerPosted Aug 11, 2021, 11:29 AM
How do i replace the old image when it's loaded into the 'pictureGallary' box?
Tanveer KhanPosted Sep 4, 2020, 5:14 AM
If i have 2 or more file as example :- 2C_temp1.dat , 2C_temp2.dat, 2C_tem3.dat, if i am going to select the file it shows error (same file name type error). What can i do so that it check the second part of file and differentiate it.
SIDDHANT AGRAWALPosted May 18, 2020, 3:25 AM
1 more requirement if you could help that how can we pass the path of the file in the adb command to send and save the file from computer to mobile. adb command - info.Arguments = "push filePath/sdcard/"; it is not workingg
SIDDHANT AGRAWALPosted May 18, 2020, 3:22 AM
Thanks a lot for above content and explanation.
Micah ParkerPosted Sep 1, 2019, 3:43 AM
Thank You! I am currently working on a series of apps that I will sell! This will be part of my image-editing/viewing program! Thanks!
Fernando GasparPosted May 1, 2019, 8:06 AM
Thank you!
Hawk eyePosted Apr 3, 2019, 5:32 AM
How do i remove the selected file once it is taken in the opendialogbox
Rushi MehtaPosted Oct 10, 2018, 3:28 AM
The nice article was written for the open dialog box
Vincent DuvernetPosted Aug 19, 2018, 4:17 PM
It seems that Path.GetFileNameWithoutExtension() associated to OpenFileDialog () is not working as expected if file extension is > 3. (In my case, I've used .scrap)
Mayzar HlaingPosted Aug 17, 2018, 6:19 AM
Hello,I have a problem concerned with open file dialog.I used ShowDialog to show the file dialog.At first, it worked correctly.After that, it exported AccessViolationException:"Attempted to read or write protected memory. This is often an indication that other memory is corrupt" .How should i solve ? I've tried many solutions.Please,help me out!
Liyana NadiaPosted May 1, 2018, 6:20 PM
Hi. I have create open file dialog in my C#. Then when i click one button it will run an extension in cmd using c code. How can i connect the open file dialog in c code with the c#?
MarianaPosted Mar 6, 2018, 3:32 AM
Hello, I have a question. How can I open image files with my own extension and visualize it in a DoubleBufferedPanel?
Rajnish JangidPosted Sep 18, 2017, 3:11 AM
Thanks lot of support And Teach Thanks Again
Anna TanPosted Jul 5, 2017, 4:00 AM
Hi. I encounter the same problem as Warren Wee. I chose an Excel File, but it doesn't appear. How to make the Excel file appear after pressing open? Thanks for your code btw.
Humayun Kabir MamunPosted Jan 3, 2016, 7:50 AM
Nice...
warren weePosted Jan 26, 2012, 8:32 PM
Hi Mahesh, I am Warren, I have some problems that I am facing now in C# programming. Anyway, I have tried your code and I was able to chose an Excel file, but it does not appear. I am not very sure is it because that the visibility is not set, in a result it does not appear. Do you have any idea why is it so? And I have afew questions about C# programming. Are you able to help me out? You can mail me back at [email protected] Sorry for any inconvenience made. Thank you in advance. Best Regards, Warren Wee
santhosh kumar akulaPosted Dec 3, 2011, 2:18 AM
in Web application useing c#,asp.net
Mahesh ChandPosted Nov 23, 2011, 2:06 AM
Guys, please post all of your questions on the forums.
Henry SmithPosted Nov 17, 2011, 8:39 AM
Thanks, IMHO, article is well presented and clear; && zip contains (is packaged as) solution/project.
armagan celikPosted Oct 29, 2011, 6:07 AM
Great job, but I want to order many many pictures side by side for continuous scrolling the all pictures. How can I do this ?