ARTICLE

OpenFileDialog and SaveFileDialog control in VB.NET

Posted by Dea Saddler Articles | ASP.NET using VB.NET March 23, 2011
In this article you will learn about OpenFileDialog and SaveFileDialog and its Common properties with an example.
Reader Level:


OpenFileDialog:

A visual basic OpenFileDialog control allow you to select a file to be opened. OpenFileDialog's are Provided by OpenFileDialog class. If you want to make visible any DialogBox at Runtime you will use the ShowDialog method of the DialogBox. If you are using OpenFileDialog control you have to set some properties like DefaultExt to set the default extension of the file, InitialDirectory to any drive(C,D,E) which should open when you use this control and RestoreDirectory to true.
SaveFileDialog:
A visual basic SaveFileDialog control is used to save a file in a specified location. This control is supported by the SaveFileDialog class. The properties of SaveFileDialog is same as the OpenFileDialog except the one notable property that is OverWritePrompt which shows a warning if you select to a name that is already exist.
Properties of OpenFileDialog and SaveFileDialog control:-

  • DefaultExt:- This property allow you to set the default file extension. 
  • InitialDirectory:- By this property you will set the InitialDirectory which should open when you use the OpenFileDialog and SaveFileDialog. 
  • RestoreDirectory:- If this property set to true it will restores the original directory before closing. 
  • FileNames:- This will gives you the file names of all selected files.

How to use OpenFileDialog and SaveFileDialog control:
  • open a new Project.
  • Drag a MenuStrip, OpenFileDialog, SaveFileDialog control on form and set the OpenFileDialog's and SaveFileDialog's property DefaultExt to txt InitialDirectory to c: and RestoreDirectory to true. The form will look like below.

     
    OpenDialog-and-SaveDialog1.gif

    OpenDialog-and-SaveDialog1..gif

    OpenDialog-and-SaveDialog2.gif
  • Write the below code

       Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs
       
    Handles SaveToolStripMenuItem.Click
            SaveFD.ShowDialog()
        End Sub
        Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Handles OpenToolStripMenuItem.Click
            OpenFD.ShowDialog()
        End
    Sub

    Output:- 

    OpenDialog-and-SaveDialog3.gif

    OpenDialog-and-SaveDialog4.gif

    OpenDialog-and-SaveDialog5.gif

    OpenDialog-and-SaveDialog7.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor