OpenFileDialog C# custom filter like 'ABC*.pdf'

user2071938 picture user2071938 · Nov 8, 2013 · Viewed 48.4k times · Source

Is it possible to specify custom filters like 'ABC*.pdf' which means: "Show all PDF which starts with ABC"?

I can only specify *.pdf, *.doc, *.*, etc.

Thanks Florian

Answer

Kjartan picture Kjartan · Nov 8, 2013

Updated

Changed my solution a little after realizing the following would be better:

This is not a complete "hard filter", but making use of the FileName property should still cover your needs:

using System;
using System.Windows.Forms;

namespace TestingFileOpenDialog
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "pro*";
            this.openFileDialog1.Filter = "Pdf Files|*.pdf";
            this.openFileDialog1.ShowDialog();
        }
    }
}

I suppose this might depend on which OS you are working with, but it did work in my case any way, on Windows 8.

I also realize that this does not filter out all irrelevant files "permanently", but it does at least provide an initial filter.

Result:
(Without pro* in the FileName-field, this will show several other PDF files).

enter image description here