SaveFileDialog shows the standard Windows “Save As” dialog and gives you back the path the user chose. It does not write anything itself; that part is your code. It lives in System.Windows.Forms, so it is Windows-only, and the project needs net8.0-windows with UseWindowsForms, which dotnet new winforms sets up:
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Save the contents of a text box
private async void SaveButton_Click(object? sender, EventArgs e)
{
using var dialog = new SaveFileDialog
{
Title = "Save notes",
Filter = "Text files (*.txt)|*.txt|Markdown files (*.md)|*.md|All files (*.*)|*.*",
DefaultExt = "txt",
FileName = "notes.txt",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};
if (dialog.ShowDialog(this) != DialogResult.OK)
{
return;
}
try
{
await File.WriteAllTextAsync(dialog.FileName, notesTextBox.Text);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
MessageBox.Show(this, ex.Message, "Could not save the file",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
async void is acceptable here because this is an event handler; the write runs without freezing the window. File.WriteAllTextAsync writes UTF-8 without a byte order mark, which is what most editors and tools expect today.
The filter string
Filter is a list of pairs separated by |: the text the user sees, then the pattern. Separate several patterns for one entry with a semicolon:
Images (*.png;*.jpg)|*.png;*.jpg|All files (*.*)|*.*
A mismatched number of | separators throws an ArgumentException when you set the property, so build long filters carefully. FilterIndex selects the entry shown first and is 1-based, not 0-based. After the dialog closes, FilterIndex tells you which entry the user picked, which is how you decide between saving as PNG or JPEG when the file name alone is ambiguous.
Properties that matter
| Property | Default | What it does |
|---|---|---|
OverwritePrompt | true | Asks before replacing an existing file. Leave it on. |
AddExtension | true | Appends DefaultExt (or the selected filter’s extension) when the user types a name without one. |
DefaultExt | empty | Extension to add, without the dot. |
FileName | empty | Suggested name, and the chosen path afterwards. |
InitialDirectory | empty | Folder to start in. Windows may ignore it and reopen the last folder used. |
CreatePrompt | false | Asks for confirmation when the file does *not* exist yet. |
RestoreDirectory | false | Restores the process’s current directory after the dialog closes. |
RestoreDirectory deserves a note: if your code opens files with relative paths, browsing to another folder in the dialog can change Environment.CurrentDirectory and break those paths later. Set it to true, or better, don’t rely on the current directory.
Writing to the stream directly
OpenFile() creates (or truncates) the selected file and returns a writable Stream, which is convenient for binary data:
if (dialog.ShowDialog(this) == DialogResult.OK)
{
using Stream stream = dialog.OpenFile();
pictureBox.Image?.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
}
Things to keep in mind
The user picked a location, but that does not mean you can write there. Read-only folders, files open in another program, a full disk or a network share that just disconnected all surface as exceptions from the write, not from the dialog. Always wrap the write in try/catch.
Common dialogs require an STA thread. The WinForms template marks Main with [STAThread], so this is only a problem if you show the dialog from a console app or a background thread, where ShowDialog throws a ThreadStateException.
For saving over an important file, consider writing to a temporary file in the same folder first and then calling File.Move(tempPath, dialog.FileName, overwrite: true). A crash halfway through then leaves the old file intact.
Thanks…