如何在 WPF 中显示另存为对话框? [英] How do I show a Save As dialog in WPF?

查看:29
本文介绍了如何在 WPF 中显示另存为对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF/C# 中有一个要求,即单击一个按钮,收集一些数据,然后将其放入一个文本文件中,用户可以将其下载到他们的机器上.我可以得到前半部分,但是你如何用另存为"对话框提示用户?该文件本身将是一个简单的文本文件.

I have a requirement in WPF/C# to click on a button, gather some data and then put it in a text file that the user can download to their machine. I can get the first half of this, but how do you prompt a user with a "Save As" dialog box? The file itself will be a simple text file.

推荐答案

到目前为止的两个答案都链接到 Silverlight SaveFileDialog 类;WPF 变体 是相当不同且不同的命名空间.

Both answers thus far link to the Silverlight SaveFileDialogclass; the WPF variant is quite a bit different and differing namespace.

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".text"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dlg.FileName;
}

这篇关于如何在 WPF 中显示另存为对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆