13

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.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Funky
  • 12,890
  • 35
  • 106
  • 161

3 Answers3

26

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
        }
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
9

Use the SaveFileDialog control/class.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
5

Im doing a notepad application in c# i came over this scenario to save file as try this out.It will work perfectly

 private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 2;
        saveFileDialog1.RestoreDirectory = true;

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {


            System.IO.StreamWriter file = new System.IO.StreamWriter(saveFileDialog1.FileName.ToString());
            file.WriteLine(richTextBox1.Text);
            file.Close();
        }



    }
rithish
  • 131
  • 1
  • 6