How to show Save As dialog in WPF?

I have a requirement in WPF / C # to click on a button, collect some data, and then put it in a text file that the user can download to his computer. I can get the first half of this, but how do you offer the user a Save As dialog box? The file itself will be a plain text file.

+85
c # save wpf
Apr 11 2018-11-11T00:
source share
6 answers

Both answers still refer to the Silverlight class SaveFileDialog ; The WPF option is quite a bit different and differs in 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; } 
+155
Apr 11 2018-11-11T00:
source share

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

+19
Apr 11 2018-11-11T00:
source share

Here is a 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); } 
+13
Apr 11 2018-11-11T00:
source share
+3
Apr 11 2018-11-11T00:
source share

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

+1
Apr 11 2018-11-11T00:
source share

All examples still 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 use the extension method to get IWin32Window from 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() also an extension method, but it is not worth mentioning that there are many examples of capitalization of the first line of a line.

0
Jan 22 '14 at 19:59
source share



All Articles