Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I show a Save As dialog in WPF?

Tags:

c#

save

wpf

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.

like image 985
Unknown Coder Avatar asked Apr 11 '11 14:04

Unknown Coder


People also ask

What is the command of Save As dialog box?

The Save As dialog box lets the user specify the drive, directory, and name of a file to save. You create and display a Save As dialog box by initializing an OPENFILENAME structure and passing the structure to the GetSaveFileName function.

Is used to open the Save As dialogue box?

By pressing F12, Save As dialogue box will open. Ctrl + O is the shortcut key to open an Open dialogue box. F12 function key is predominantly used in Microsoft Office.

How do I save a dialog box save in C#?

To save a file using the SaveFileDialog component. Display the Save File dialog box and call a method to save the file selected by the user. Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to.


1 Answers

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; } 
like image 92
Aaron McIver Avatar answered Oct 24 '22 03:10

Aaron McIver