C# 在“保存文件”提示中使用适当的扩展名保存文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1213339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 11:05:14  来源:igfitidea点击:

Save file with appropriate extension in a Save File prompt

c#winformsfile-extensionsavefiledialog

提问by novacara

In my application I use a SaveFileDialog to pop up a Save As window. I have restricted in the file type section the file to be saved as .dat with the following code.

在我的应用程序中,我使用 SaveFileDialog 弹出一个另存为窗口。我在文件类型部分限制了使用以下代码保存为 .dat 的文件。

sfdialog.Filter = "Data Files (*.dat*)|*.dat*";

What I want to know how to do is make it automatically save with the .dat extension. Currently it just saves with no extension unless I specifically save it as filename.dat.

我想知道怎么做是让它自动以 .dat 扩展名保存。目前它只是不带扩展名保存,除非我专门将它保存为 filename.dat。

采纳答案by Francis B.

SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Data Files (*.dat)|*.dat";
dlg.DefaultExt = "dat";
dlg.AddExtension = true;

回答by John Calsbeek

The AddExtensionand DefaultExtproperties. For example:

AddExtensionDefaultExt性能。例如:

sfdialog.DefaultExt = "dat";
sfdialog.AddExtension = true;