Winforms Save as

淺唱寂寞╮ 提交于 2020-06-13 19:08:02

问题


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.


回答1:


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



回答2:


Use the SaveFileDialog control/class.




回答3:


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



    }


来源:https://stackoverflow.com/questions/5067662/winforms-save-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!