单击消息框的确定时如何打开保存文件对话框

Dor*_*eka 1 c# desktop-application

当我点击显示的消息框时,我想提示用户输入保存文件对话框.我怎样才能做到这一点...

Alb*_*nbo 6

在按钮的事件处理程序中,使用以下代码.

DialogResult messageResult = MessageBox.Show("Save this file?", "Save", MessageBoxButtons.OKCancel);
if (messageResult == DialogResult.OK)
{
    using (var dialog = new System.Windows.Forms.SaveFileDialog())
    {
        dialog.DefaultExt = "*.txt";
        dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
        DialogResult result = dialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            string filename = dialog.FileName;
            // Save here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:如果你想直接获得FileStream,你可以使用SaveFileDialog.OpenFile().如果以部分信任方式运行应用程序,则需要较少的权限.