我使用了这里的信息 http://msdn.microsoft.com/ru-ru/library/system.windows.forms.openfiledialog(v=vs.110).aspx

这边走:

Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

 dlg.DefaultExt = ".xml"; // this is how I get only required extension
 dlg.Filter = "XML files (*.xml)|*.xml"; // I guess, this should be modified, don't know how.
dlg.InitialDirectory = _directoryName1;
// here we go
Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string path = dlg.FileName;

在初始目录中,我必须输入具有相同 xml 扩展名的文件类型,名称以 script-Data...GeneralParam... 开头。所以我只需要在 OpenFileDialog 中显示文件,名称以 script-Data... 开头。

我知道,我可以通过解析 path 来通知用户他确定了错误的文件,但这对我来说不是一个好的解决方案。这里还有其他出路吗?

最佳答案

您已经设置了 Filter 属性。所以当 .xml 打开时你只能看到 OpenFileDialog 文件。但是如果你想filter filenames 显示在OpenFileDialog 你可以设置FileName 属性,因为filter 没有其他filename 选项

试试这个:

dlg.FileName = "script-Data*";

关于c# - 如何限制应在 OpenFileDialog 中显示哪些文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20439636/

10-13 06:58