user2071938
user2071938

Reputation: 2255

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

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

Upvotes: 20

Views: 61673

Answers (6)

Lukas
Lukas

Reputation: 103

I checked it again and it seems for .NET 5.0 it works just as OP expected it.

var fileSelector = new OpenFileDialog()
{
  Filter = "ABC Files|abc*.pdf";  
};

With this the FileDialog will only show .pdf files beginning with "abc".

Upvotes: 0

Kjartan
Kjartan

Reputation: 19111

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

Upvotes: 38

jAC
jAC

Reputation: 5324

As stated in my comment:

Unfortunately it's not possible. But you can create your own FileDialog

To create your own FileDialog, you can use the following methods:

  string[] Directories = Directory.GetDirectories(Path);
  string[] Files = Directory.GetFiles(Path);

Now filter the Files-Array to your specifications:

List<string> wantedFiles = Files.ToList().Where(x => x.StartsWith("ABC"));

To get the file Icons, you have to use the DLLImport of Shell32.dll:

[DllImport("shell32.dll")]

The code provided in this SO question should solve the problem.

A project that implements own FileDialogs written by my brother can be found here: Download project

In short, this should do the trick:

foreach (string file in Directory.GetFiles(Path)
                                 .Where(x => new DirectoryInfo(x).Name.StartsWith("ABC")))
{
    //Add the string to your ListView/ListBox/...
}

Upvotes: 3

rPulvi
rPulvi

Reputation: 946

Use this:

Microsoft.Win32.OpenFileDialog myDialog. = new Microsoft.Win32.OpenFileDialog();
myDialog..DefaultExt = ".pdf";
myDialog.Filter = "FilesIWant (ABC*.pdf)|ABC*.pdf

Upvotes: -2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Answer is straight forward : NO

You can set the Filters to allow only specific File Types with property Filter asbelow :

fileOpenDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

but filtering file names is NOT Possible.

You can create your own Custom OpenFIleDialog in that case.

See this Link for more info : How to create customized open file dialog in C#

Upvotes: 1

Noctis
Noctis

Reputation: 11763

Yes and no.

No: Look at the MSDN, page. The filter is not used that way. It's only for the extensions.

Yes: You could write your own class that extends/mimics the OpenFileDialog, have some regular expressions to do what you want, and simply run that match against all the files in the current folder (Might take some work, but if you really want it so bad, go for it :) )

Upvotes: 3

Related Questions