SaveFileDialog我应该使用FilterIndex属性还是FileName属性的扩展名? [英] SaveFileDialog should I use the FilterIndex property or the extension of the FileName property?
问题描述
前几天,我遇到以下问题 SaveFileDialog AddExtension无法按预期工作.现在我想到了一个后续问题.
The other day I had the following question SaveFileDialog AddExtension doesn't work as expected. Now a follow up question came to my mind.
我应该使用 FilterIndex 属性或 FileName <的扩展名 SaveFileDialog 的/a>属性确定我要以哪种文件格式存储数据?
Should I use the FilterIndex property or the extension of the FileName property of the SaveFileDialog to decide under which file format I want to store the data?
我有以下C#测试代码:
I've the following C# test code:
var dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "txt";
dialog.Filter = "Text files (*.txt)|*.txt|XML files (*.xml)|*.xml";
dialog.OverwritePrompt = true;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string extension = System.IO.Path.GetExtension(dialog.FileName);
int filterIndex = dialog.FilterIndex;
}
文档引用以下内容:
显示文件后,您也可以使用FilterIndex的值根据过滤器执行特殊文件操作的对话框选择.
You can also use the value of FilterIndex after showing the file dialog to perform special file operations depending upon the filter chosen.
如果我使用 FilterIndex
属性,它将保存例如带有XML扩展名的文本文档(对话框 File name = test7.xml
,对话框 Save as类型= * .txt
).
If I use the FilterIndex
property it will save for example a text document with an XML extension (Dialog File name = test7.xml
, Dialog Save as type = *.txt
).
如果我使用 FileName
的扩展名,则对话框的另存为类型
将被忽略.
If I use the extension of the FileName
then the Save as type
of the dialog is ignored.
推荐答案
文件名和格式之间存在差异.
There's a difference between the file's name and its format.
FilterIndex属性可以指定文件的格式,但是FileName应该指定他们希望文件如何命名.这些可以不同.
The FilterIndex property can specify the format of the file, but the FileName should specify how they want the file named. These can be different.
但是要当心.如果格式列表中包含"所有文件(*.*)
"的选项,则会遇到默认格式的问题.这种格式对用户来说很明显吗?
Beware, however. If your list of formats includes an option for "All Files (*.*)
", you run into an issue with the default format. Is that format obvious to the user?
简而言之,我对您的建议是使用下拉列表作为格式,并使用FileName.让用户保存扩展名为.TXT的CSV文件.
My suggestion to you, in short, is to use the drop-down list for the format, and the FileName for just that. Let a user save a CSV file with a .TXT extension.
这篇关于SaveFileDialog我应该使用FilterIndex属性还是FileName属性的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!