Directory searching

Using:

Directory.GetFiles(string path, string searchPattern)

Is there a way to specify multiple file types in the search pattern What I'm trying to do is get all BMP, PNG, and JPG files from a directory without having to search the same directory three times to get all of the files.

Thanks in advance,
Bill



Answer this question

Directory searching

  • dotnetgruven

    Thanks for the info.

    This is what I ended up doing:

    string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
    foreach (string s in files)
    {
    string extension = s.Substring(s.LastIndexOf("."));
    if (".jpg .bmp .png .gif".IndexOf(extension) > -1)
    {
    //Other code
    }
    }

    This seems to do what I want.

    Bill


  • Tonia

    I don't think so it is possible for provide multiple search patterns like "*.jpg | *.bmp". i faced this problem also a little while ago, but was unable to find any solution.

    Workaround : Get all the files from the directory and then compare each file extension


  • jonsofield

    It is not possible to specify mltiple search patterns, since the search string parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.

    For more Info. about this you can check the link from MSDN

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfSystemIODirectoryClassGetFilesTopic2.asp

    WorkAround : You can use some recursion and File events to achieve this.



  • Directory searching