4

为什么过滤器扩展不显示OpenFileDialog?我对此进行了测试,但对话框未显示DAT扩展名。如果我DAT在列表扩展的第一个中获得扩展,则OpenFileDialog在对话框中进行过滤。我C#在 Win XP 上使用 Application-FrameWork 3.5。

这是我的代码:

OpenFileDialog openFileDialog = new OpenFileDialog();

string VideoFormat = "Video files |*.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                          " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm; *.dat; ";


openFileDialog.Filter = VideoFormat;
openFileDialog.ShowDialog();

但是这段代码在对话框中过滤了 DAT 扩展:

OpenFileDialog openFileDialog = new OpenFileDialog();

            string VideoFormat = "Video files | *.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                      " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm; ";


openFileDialog.Filter = VideoFormat;
openFileDialog.ShowDialog();
4

1 回答 1

7

这是OpenFileDialog对象的 Filter 属性的正确格式:

(*.jpg)|*.jpg|Image Files (*.png)|*.png|Image Files (*.jpeg)|*.jpeg

尝试这个:

        OpenFileDialog openFileDialog = new OpenFileDialog();
        string formats = "*.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                  " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm";

        string[] exts = formats.Split(';');
        string filter = string.Empty;
        foreach (string ext in exts)
        {

                filter += "Video Files (" + ext.Replace("*", "").Trim() + ")|" + ext + "|";
        }

        openFileDialog.Filter = filter.Remove(filter.Length-1,1);
        openFileDialog.ShowDialog();

更新

此选项仅显示一个选项,但可让您选择所有视频类型:

        OpenFileDialog openFileDialog = new OpenFileDialog();
        string formats = "All Videos Files |*.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                  " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm";

        openFileDialog.Filter = formats;
        openFileDialog.ShowDialog();

它必须是那样的。

于 2013-01-28T22:27:56.463 回答