SaveFileDialog设置默认路径和文件类型? [英] SaveFileDialog setting default path and file type?

查看:882
本文介绍了SaveFileDialog设置默认路径和文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SaveFileDialog.SaveFile 。如何将其获取到默认(操作系统)驱动器号,并同时限制选项以仅显示 .BIN 作为文件扩展名?

I'm using SaveFileDialog.SaveFile. How can I get it to the default (operating system) drive letter and also limit the options to show only .BIN as the file extension?

我尝试阅读MSDN上的文档,但是我对此很陌生,老实说,我发现它们有时不清楚。

I tried reading the docs on MSDN but I'm very new to this and to be honest I find them sometimes unclear.

推荐答案

SaveFileDialog 控件根本不会进行任何保存。它的作用是为您提供一个方便的界面,以实际显示Windows的默认文件保存对话框。

The SaveFileDialog control won't do any saving at all. All it does is providing you a convenient interface to actually display Windows' default file save dialog.

1)设置属性 InitialDirectory 到您想要显示其他默认值的驱动器。试想一下可能具有不同布局的其他计算机。默认情况下,Windows将保存上次使用的目录,然后再次显示。

1) Set the property InitialDirectory to the drive you'd like it to show some other default. Just think of other computers that might have a different layout. By default windows will save the directory used the last time and present it again.

2)在控件之外处理。您必须检查对话框的结果,然后进行保存(例如,编写文本或二进制文件)。

2) That is handled outside the control. You'll have to check the dialog's results and then do the saving yourself (e.g. write a text or binary file).

只是一个简单的例子(有其他方法)去做吧)。
保存文件是类型为 SaveFileDialog

Just as a quick example (there are alternative ways to do it). savefile is a control of type SaveFileDialog

SaveFileDialog savefile = new SaveFileDialog(); 
// set a default file name
savefile.FileName = "unknown.txt";
// set filters - this can be done in properties as well
savefile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";

if (savefile.ShowDialog() == DialogResult.OK)
{
    using (StreamWriter sw = new StreamWriter(savefile.FileName))
        sw.WriteLine ("Hello World!");
}

这篇关于SaveFileDialog设置默认路径和文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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