C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Also: Double-click on the SaveFileDialog icon in your Visual Studio designer window as well to add the FileOk event handler.
Click: The button1_Click event handler was added, and the saveFileDialog1_FileOk event handler was added.
Info: In the button1_Click method, we simply call the ShowDialog method on the saveFileDialog1 instance.
And: This will make the save file dialog appear on the user's screen when he or she presses the button.
C# program that uses SaveFileDialog
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication30
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// When user clicks button, show the dialog.
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
// Get file name.
string name = saveFileDialog1.FileName;
// Write to the file name selected.
// ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test");
}
}
}
But: Typically you will want to read the FileName property from the saveFileDialog1 instance.
Properties:
AddExtension
CheckFileExists
CheckPathExists
CreatePrompt
DefaultExt
Filter
FilterIndex
OverwritePrompt
RestoreDirectory
ShowHelp
SupportMultiDottedExtensions
ValidateNames