Winforms Save as

Funky picture Funky · Feb 21, 2011 · Viewed 15.3k times · Source

Does anyone know any articles or sites showing how to create a "Save as" dialogue box in win forms. I have a button, user clicks and serializes some data, the user than specifies where they want it saved using this Save as box.

Answer

Jon Skeet picture Jon Skeet · Feb 21, 2011

You mean like SaveFileDialog?

From the MSDN sample, slightly amended:

using (SaveFileDialog dialog = new SaveFileDialog())
{
    dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
    dialog.FilterIndex = 2 ;
    dialog.RestoreDirectory = true ;

    if (dialog.ShowDialog() == DialogResult.OK)
    {
        // Can use dialog.FileName
        using (Stream stream = dialog.OpenFile())
        {
            // Save data
        }
    }
}