How to open a file picker (open/save file) dialog at runtime

Posted in: General vb.NET
By dePoPo
Mar 6, 2009 - 12:10:36 PM


And updated version of this article can be found here

This code opens a filepicker dialog at runtime



        Dim s_filename As String = ""
        Dim myopener As New OpenFileDialog
        myopener.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        If myopener.ShowDialog = Windows.Forms.DialogResult.OK Then
            s_filename = myopener.FileName
        Else
            MsgBox("No file selected")
        End If

Example using a save file dialog with a default directory

        Dim mysavedialog As New SaveFileDialog
        mysavedialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*"
        mysavedialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        If mysavedialog.ShowDialog() = System.Windows.Forms.DialogResult.Cancel Then Exit Sub

        Dim s_exportfile As String = mysavedialog.FileName


Visitor Comments