How do I show a Save As dialog in WPF?

C#WpfSave

C# Problem Overview


I have a requirement in WPF/C# to click on a button, gather some data and then put it in a text file that the user can download to their machine. I can get the first half of this, but how do you prompt a user with a "Save As" dialog box? The file itself will be a simple text file.

C# Solutions


Solution 1 - C#

Both answers thus far link to the Silverlight SaveFileDialogclass; the WPF variant is quite a bit different and differing namespace.

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".text"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

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

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dlg.FileName;
}

Solution 2 - C#

SaveFileDialog is in the Microsoft.Win32 namespace - might save you the 10 minutes it took me to figure this out.

Solution 3 - C#

Here is some sample code:

string fileText = "Your output text";

SaveFileDialog dialog = new SaveFileDialog()
{
    Filter = "Text Files(*.txt)|*.txt|All(*.*)|*"
};

if (dialog.ShowDialog() == true)
{
     File.WriteAllText(dialog.FileName, fileText);
}
 

Solution 4 - C#

Solution 5 - C#

You just need to create a SaveFileDialog, and call its ShowDialog method.

Solution 6 - C#

All the examples so far use the Win32 namespace, but there is an alternative:

FileInfo file = new FileInfo("image.jpg");
var dialog = new System.Windows.Forms.SaveFileDialog();
dialog.FileName = file.Name;
dialog.DefaultExt = file.Extension;
dialog.Filter = string.Format("{0} images ({1})|*{1}|All files (*.*)|*.*",
    file.Extension.Substring(1).Capitalize(),
    file.Extension);
dialog.InitialDirectory = file.DirectoryName;

System.Windows.Forms.DialogResult result = dialog.ShowDialog(this.GetIWin32Window());
if (result == System.Windows.Forms.DialogResult.OK)
{

}

I'm using an extension method to get the IWin32Window from the visual control:

#region Get Win32 Handle from control
public static System.Windows.Forms.IWin32Window GetIWin32Window(this System.Windows.Media.Visual visual)
{
    var source = System.Windows.PresentationSource.FromVisual(visual) as System.Windows.Interop.HwndSource;
    System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle);
    return win;
}

private class OldWindow : System.Windows.Forms.IWin32Window
{
    private readonly System.IntPtr _handle;
    public OldWindow(System.IntPtr handle)
    {
        _handle = handle;
    }

    System.IntPtr System.Windows.Forms.IWin32Window.Handle
    {
        get { return _handle; }
    }
}
#endregion

Capitalize() is also an extension method, but not worth mentioning as there are plenty of examples of capitalizing the first letter of a string out there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUnknown CoderView Question on Stackoverflow
Solution 1 - C#Aaron McIverView Answer on Stackoverflow
Solution 2 - C#upsidedowncreatureView Answer on Stackoverflow
Solution 3 - C#RQDQView Answer on Stackoverflow
Solution 4 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 5 - C#Twelve47View Answer on Stackoverflow
Solution 6 - C#Chuck SavageView Answer on Stackoverflow