SIGN UP MEMBER LOGIN:    
ARTICLE

OpenFileDialog in C#

Posted by Robert Pattinson Articles | Visual C# June 09, 2009
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.
Reader Level:

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.

    private void 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 = new OpenFileDialog();

 

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

 

share this article :
post comment
 

tell me chat room in C# with help of asp.net

Posted by Sanju Agrawal May 19, 2012

sir pls tell me how to make own browser in C#.........

Posted by Sanju Agrawal May 19, 2012

Sir can u tell me chat room in C# with Help of Asp.net.....

Posted by Sanju Agrawal May 19, 2012

Good Article I like it

Posted by Sanju Agrawal May 19, 2012

Good Article I like it

Posted by Sanju Agrawal May 19, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor