Domanda

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.

È stato utile?

Soluzione

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
        }
    }
}

Altri suggerimenti

Use the SaveFileDialog control/class.

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();
        }



    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top