Get Started with the SaveFileDialog and OpenFileDialog – VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.13 out of 5)

The Save and Open File Dialogs are easy to implement and use. The hardest part is setting up the filter the way you want it to be. Even that will be considered easy after you see the format that’s used.

First of all, this is just a small article and not a complete overview of the Dialog objects. It is simply to help you get started. I will go over the most used features of the OpenFileDialog and SaveFileDialog objects. The code/comments in this article will work in Visual Basic.NET, Visual Basic 2008, VB 2010, and higher.

Your IDE toolbox should by default contain the OpenFileDialog and SaveFileDialog controls. You could just double click those to add them to your project.

But I prefer to create and initialize mine through code like below…

        Dim OpenDLG As New OpenFileDialog

        Dim SaveDLG As New SaveFileDialog

The first feature I will mention is the .AddExtension property. That simply means that you can tell the dialog whether or not to add a extension if the user omits it. In other words, if your filter was setup for Jpeg files, and the user only put the filename without the .jpg, the dialog would automatically add the .jpg extension to it. I normally set this to true.

The next items are CheckFileExists and CheckPathExists. They are self explanatory and not really anything to say. It will simply check if the path or filename exists. I just leave these at default.

The next item I sometimes use, is .DefaultExtension. IF no extension is supplied, then the dialog will use the extension you supplied in this DefaultExtension property.

Filename is the next highly used feature and it is exactly what it says. This is the path and filename that the user selected.


       Text1.Text = OpenDLG.FileName

That code will put the full path and filename the user selected in the textbox control.

The .Filenames() feature is used if you set the Dialog to allow the user to select Multiple files at a time.

The next common feature is the Filter property. This is where you tell the dialog which file types to show in the selection window. If you only want the user to see JPeg files then you would use this feature to show files with the .jpg extension.

So if you only wanted the user to see JPeg files with the extension “jpg”, you would setup the filter as below…

        OpenDLG.Filter = "JPEG Files (*.jpg)|*.jpg"

When the dialog is shown, the Dialog would allow only files with the .jpg extension to be displayed to the user.

What if you want the user to select multiple types image formats? It is similar to the above.


        OpenDLG.Filter = "Images Files (*.bmp, *.gif, *.jpg)|*.bmp;*.gif;*.jpg"

That will allow the user to select only bitmap, gif, and/or jpeg files.

Lets say you want the user to be able to do all 3 of the extensions, but only one Extension at any given time and allow them to pick from the box what extension they want? It works as outlined below…

        OpenDLG.Filter = "Bitmap Files (*.bmp)|*.bmp|Gif Files (*.gif)|*.gif|JPeg Files (*.jpg)|*.jpg"

When you show that dialog, then the Combobox will have those 3 formats available as separate extension items and will allow your user to select which to use.

What if you want the user to see all files that have any kind of extensions? See below…

        OpenDLG.Filter = "All Files (*.*)|*.*"

OK, what if you want one of the 3 extensions as outlined above to be the first extension that is selected in the dialogs Combobox? That is where another feature comes in: .FilterIndex.  IF you wanted the 3rd extension in the box to be selected when the Dialog is open, then just do…

        OpenDLG.FilterIndex = 3

The next useful feature is the .InitialDirectory property. IT is the directory path you want the user to be at when they first open the Dialogbox.

        OpenDLG.InitialDirectory = "c:\mypath\path"

The next is .Multiselect. You simply tell the dialog whether the user can select more than 1 file or not.

You can also reset all properties to their default values using the .Reset method.

        OpenDLG.Reset() '– will set the Dialogs properties to the default settings.

Then the next is .ShowDialog. As the name suggests, it will display the Open or Save Dialog window to the user.

Simply call it like this…


It will then show the dialog window.

But how do you know if the user pressed Open or if they pressed Cancel? Check out the code below…

        DialogResult = OpenDLG.ShowDialog

DialogResult will contain the result after the Dialog has been closed. So in other words if the user clicked Cancel, then DialogResult would would have that data.


        DialogResult = OpenDLG.ShowDialog()

        If DialogResult = Windows.Forms.DialogResult.OK Then

            MsgBox("User Pressed Open/OK!")

        ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then

            MsgBox("User canceled the action!")

        End If

After the user pressed Cancel or Open, it would throw one of the Messages depending on which was clicked.

One last feature is the Title feature. That is the text the user will see in the Dialogs titlebar  when it opens.

        OpenDLG.Title = "Here is MY Dialog!!"

Well I hope this brief tutorial was able to help you get an understanding of the File Dialog. Have fun!


Revised: 2015

7 thoughts on “Get Started with the SaveFileDialog and OpenFileDialog – VB.NET and Higher

  1. JG

    Dude, this code was pretty useless. I came here from a Google search trying to find out how to code the actual SaveFileDialog stuff and this just gives a basic overview of the options, it doesn’t talk at all about making the save actually work.

  2. Darvin

    JG, The Save File Dialog does not actually save the file. It is just a control to help you pick a file name to save to. Another object has to actually save the file. For a text file it would be System.IO.StreamWriter, for instance.

  3. Agustin


    I’m a bit confused with the title of your tutorial, it was saying that both openfiledialog and savefiledialog would be used, so anyway, I was wondering if you could point me towards where to learn how to save an actual file? I’m trying to save an xlsx (excel) file to later be used…

    Thank you kindly for your time and consideration

    yours truly,


  4. Jason Post author

    Hi Agustin, this article was on using the OpenFile and SaveFile dialog objects. Basically they are to provide a user interface for the user to select or specify a filename and path to/for a file.

    So you would have to write your own code to handle saving or opening the actual file itself. There are alot of options under the System.IO namespace for you to check out. Like the StreamWriter/StreamReader and so on.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.