0

I am trying to get the handler to a SaveFileDialog which is opened by a button click of my WPF app. All the examples that I could find in the net actually creates one , but I need to handle one which is already opened. How do I do that? The below code always creates a new SaveFileDialog

        dlg.DefaultExt = "pdf"; // Default file extension
        dlg.Filter = "PDF File (*.pdf)|*.pdf|All files (*.*)|*.*"; // Filter files by extension
        dlg.FilterIndex = 2;
        dlg.InitialDirectory = "C:\\Users\\Reema.Sinha\\Downloads";
        Manager.Current.DialogMonitor.AddDialog(dlg);
        DownloadSaveButton.Click();
        DialogResult result = dlg.ShowDialog();
Reema
  • 969
  • 1
  • 10
  • 17
  • If I got you right, You mean something like if you hit cancel than it should pop up a message box asking for confirmation.. And if clicked save than it should ask for something else...? – StackUseR Dec 05 '16 at 15:49
  • Nope.I hit the download button,automatically a SaveFileDialog opens up and I need a handler to it to set the path name and then save the file as a pdf. – Reema Dec 05 '16 at 15:57

1 Answers1

-1

I guess OpenFileDialog can also do the trick. But I will prefer SaveFileDialog only. I tried this and hope it works for you also:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = "pdf"; // Default file extension
dlg.Filter = "PDF File (*.pdf)|*.pdf|All files (*.*)|*.*"; // Filter files by extension
    dlg.FilterIndex = 2;
    dlg.InitialDirectory = "C:\\Users\\Reema.Sinha\\Downloads";
    Manager.Current.DialogMonitor.AddDialog(dlg);
    DownloadSaveButton.Click();

   Nullable<bool> result = dlg.ShowDialog();  // Show save file dialog box

  if (result == true)
  {
    // Save document
    string filename = dlg.FileName;
  }

UPDATE:

Use FolderBrowserDialog. Then after you show the dialog to the user, you can do dlg.SelectedPath.

Hopefully these posts are helpfull to you.

SaveFileDialog that permits selection of folder

WPF select folder dialog

Open directory dialog

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
StackUseR
  • 884
  • 1
  • 11
  • 40
  • i had tried this before, but the issue is with : Manager.Current.DialogMonitor.AddDialog(dlg); dlg has to implement IDialog, but SaveDialog is no way connected to IDialog, and hence Conversion is not possible here. – Reema Dec 05 '16 at 22:29
  • I found the SaveAsDialog and it works just fine. Thanks anyways for the help. – Reema Dec 07 '16 at 19:59
  • 1
    Great.:) upload the answer. It would be helpful to others. – StackUseR Dec 08 '16 at 02:45