Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding .EndsWith() from combobox

Tags:

c#

winforms

I have a combo box called comboFileTypes. Inside that is a drop down list containing:

MP4
MOV
MKV
VOB

And after a button press I have the following code to scan a directory for files:

var files = Directory
    .EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
    .Where(s => 
        s.EndsWith(".mp4") || 
        s.EndsWith(".mov") || 
        s.EndsWith(".vob") ||
        s.EndsWith(".MP4") || 
        s.EndsWith(".MOV") || 
        s.EndsWith(".VOB"));

Which is hardcoded. I want the WHERE option to be dynamically generated from the combobox instead, so that the user can add another type of file if they need to. (Also case insensitive, if that's possible, otherwise I'll just add both cases)

Any help would be appreciated.

like image 699
Chud37 Avatar asked Jan 16 '19 09:01

Chud37


1 Answers

You can get values from ComboBox by

var values = comboFileTypes.Items.Cast<string>()

and use it in like this:

var files = Directory
    .EnumerateFiles(sourceDIR.Text, "*.*", SearchOption.AllDirectories)
    .Where(s => values.Any(v => s.EndsWith(v, StringComparison.OrdinalIgnorecase));
like image 141
Marcin Topolewski Avatar answered Nov 01 '22 01:11

Marcin Topolewski