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, 5:04 PM EST
If (*variable* is null){ btnSaveAs_Click(sender, e) } else { method(variable) }
Todd HoatsonPosted Oct 7, 2021, 5:48 PM EDT
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, 7:29 AM EDT
How do i replace the old image when it's loaded into the 'pictureGallary' box?
Tanveer KhanPosted Sep 4, 2020, 1:14 AM EDT
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 17, 2020, 11:25 PM EDT
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 17, 2020, 11:22 PM EDT
Thanks a lot for above content and explanation.
Micah ParkerPosted Aug 31, 2019, 11:43 PM EDT
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, 4:06 AM EDT
Thank you!
Hawk eyePosted Apr 3, 2019, 1:32 AM EDT
How do i remove the selected file once it is taken in the opendialogbox
Rushi MehtaPosted Oct 9, 2018, 11:28 PM EDT
The nice article was written for the open dialog box
Vincent DuvernetPosted Aug 19, 2018, 12:17 PM EDT
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, 2:19 AM EDT
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, 2:20 PM EDT
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 5, 2018, 10:32 PM EST
Hello, I have a question. How can I open image files with my own extension and visualize it in a DoubleBufferedPanel?
Rajnish JangidPosted Sep 17, 2017, 11:11 PM EDT
Thanks lot of support And Teach Thanks Again
Anna TanPosted Jul 5, 2017, 12:00 AM EDT
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, 2:50 AM EST
Nice...
warren weePosted Jan 26, 2012, 3:32 PM EST
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 2, 2011, 9:18 PM EST
in Web application useing c#,asp.net
Mahesh ChandTOP 10Posted Nov 22, 2011, 9:06 PM EST
Guys, please post all of your questions on the forums.
Henry SmithPosted Nov 17, 2011, 3:39 AM EST
Thanks, IMHO, article is well presented and clear; && zip contains (is packaged as) solution/project.
armagan celikPosted Oct 29, 2011, 2:07 AM EDT
Great job, but I want to order many many pictures side by side for continuous scrolling the all pictures. How can I do this ?