0

I want to be able to display two file extensions in the listbox but apparently the code only works if i place one value (ex: string extension = "*.png";) on my code.

        string path = cmbDrive.Text;
        string extension = "*.txt" + "*.png";
        foreach (string s in FileUts.GetFiles(path, extension))
        {
            lbDBview.Items.Add(s);
        }
        if (lbDBview.Items.Count == 0) 
        {
            MessageBox.Show("No Files found!");
        }
  • Here is a similar question http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters – TGH Oct 20 '14 at 03:52

1 Answers1

3

It's not clear what FileUts.GetFiles() is doing exactly.

If it is calling OpenFileDialog try

extension = "TXT|*.txt;PNG|*.png"

If it is calling Directory.GetFiles(), you cannot specify multiple criteria to searchPattern. However, you could combine two results, e.g.

foreach (string s in FileUts.GetFiles(path, extension1)
                     .Union(FileUts.GetFiles(path, extension2)))
{
    // Do stuff
}

Note also the comment, that you can make a single call to Directory.GetFiles() and use Linq, assuming nothing else interesting happens in FileUts.GetFiles().

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553