How to display a SaveAs dialog box in VB.NET WPF

By Allen Stoner

WPF no longer uses the Common dialog boxes like VB.NET has used in the past. There is a similar form available, it's just not made available via the toolbox for dropping on a form. This code uses the path and file name in the txtFileName text box to populate the initial values of the dialog then sets the returning value back into the text field. It also saves the directory to an application setting to use the next time the program runs, so it remembers the directory it was pointed at.

        Dim dlg As New Microsoft.Win32.SaveFileDialog
        dlg.InitialDirectory = Path.GetDirectoryName(txtFilename.Text)
        dlg.FileName = Path.GetFileName(txtFilename.Text)
        dlg.DefaultExt = ".csv" ' Default file extension
        dlg.Filter = "CSV Files (.csv)|*.csv" ' Filter files by extension

        ' Show open file dialog box
        Dim result? As Boolean = dlg.ShowDialog()

        ' Process open file dialog box results
        If result = True Then
            ' Open document
            txtFilename.Text = dlg.FileName
            My.Settings("DefaultPath") = Path.GetDirectoryName(txtFilename.Text) & "\"
            My.Settings.Save()
        End If

How to display a SaveAs dialog box in VB.NET WPF  (2368 Views)