WPF - SaveFileDialog WPF - SaveFileDialog wpf wpf

WPF - SaveFileDialog


SaveFileDialog will return true if user saved (the ShowDialog method returns a nullable bool), and return false/null if user pressed cancel. Below is a sample MSDN code to get you started:

// Configure save file dialog boxMicrosoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();dlg.FileName = "Document"; // Default file namedlg.DefaultExt = ".txt"; // Default file extensiondlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension// Show save file dialog boxNullable<bool> result = dlg.ShowDialog();// Process save file dialog box resultsif (result == true){    // Save document    string filename = dlg.FileName;}


You need to make use of MessageBox in WPF to open another window when users click cancel. Add the following code to the cancel button event :-

private void canceButton()    {        MessageBoxResult key = MessageBox.Show(            "Are you sure you want to quit",            "Confirm",            MessageBoxButton.YesNo,            MessageBoxImage.Question,            MessageBoxResult.No);        if (key == MessageBoxResult.No)        {            return;        }        else        {            Application.Current.Shutdown();        }    }


You have to use DialogResult Property for using a dialog result in WPF. For more information on using dialogresult in WPF refer to WPF Dialogs and DialogResult