Winforms select 用于保存文档的文件名

Winforms select filename for saving document

我有一些数据,我想将其导出到 excel。我完成了所有代码,一切正常,现在我想将 excel 文件保存到硬盘。我也能做到。但我的问题是我不知道如何让客户设置 his/her 自己的文件名。

我尝试过的:

FolderBrowserDialog brwsr = new FolderBrowserDialog();

//Check to see if the user clicked the cancel button
if (brwsr.ShowDialog() == DialogResult.Cancel)
    return;
else
{
    string newDirectoryPath = brwsr.SelectedPath;
    //Do whatever with the new path
}

该方法的问题在于它只允许用户 select 将文件保存到的文件夹。我希望所有用户指定路径 文件名。

有什么想法可以帮助请求吗?

非常感谢

您需要改用 SaveFileDialog。这让用户指定路径+文件名。查看此以获取更多信息:SaveFileDialog on MSDN

SaveFileDialogFolderBrowserDialog 非常相似,因此您几乎可以替换已有的代码 ;)

指定文件类型(编辑)

看看 Filter property

Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*