Using GetFiles

I want to retrive all the files in a directory of certian types (.bmp, .jpg, .gif) etc etc. But I seem to only be able to pass in a single filter to the function, is this by design

filesInDirectory = Directory.GetFiles(PictureDirectory, "*.jpg");

Works fine but:

filesInDirectory = Directory.GetFiles(PictureDirectory, "*.jpg;*.bmp");

Does not work correctly at all, Is there any way to do this, without calling multiple times and joining arrays

Also it seems to just return them in alphabetical order, is there anyway to return them in the way they are sorted/being viewed in Explorer

Thanks!


Answer this question

Using GetFiles

  • Keyser

    The order they are returned in is probably dependent upon the order in which the file system stores them in its internal directory list.  Explorer sorts based on the order determined by the user and is dependent upon the user and folder being viewed.  There is no direct way of obtaining this information without going to Explorer directly.

    As for multiple extensions no Directory doesn't support it.  The biggest issue is that ; is a valid filename character so it would have no way of knowing that you wanted multiple extensions vs. a single one.  You'd have to use an overload that accepted an array of extensions to search for.  Nevertheless it is pretty easy to do what you want (perhaps wrapping it in a custom function for others).

    public static string[] GetFiles ( string pathName, params string[] patterns )
            {
                StringCollection coll = new StringCollection();

                foreach (string pattern in patterns)
                {
                    string[] files = Directory.GetFiles(pathName, pattern);
                    coll.AddRange(files);
                };

                string[] arr = new string[coll.Count];
                coll.CopyTo(arr, 0);
                return arr;
           }

    Of course you can sort the array however you want.

    Michael Taylor - 11/26/05


  • noslen

     TaylorMichaelL wrote:
    The order they are returned in is probably dependent upon the order in which the file system stores them in its internal directory list.  Explorer sorts based on the order determined by the user and is dependent upon the user and folder being viewed.  There is no direct way of obtaining this information without going to Explorer directly.

    Thanks! GetFiles always seems to return the items in alphabetical order. Are there other ways to return files from a directory (or simply the names). Where I got this idea was from the Windows Picture and Fax Viewer. Whenever you flip through the images it's always in the exact order that it's displayed in the folder. I don't know if this is how functions like FindNextFile in Win32 work or if they are doing something special.

    Just wondering if anyone might have some ideas to try.


  • markiemarkie

    MSDN Magazine has this month a wrapper class for FindFirstFile/FindNextFile that you may find useful; I think it looks a lot more flexible and efficient than the standard .Net offerings:

    Here it is.


  • Using GetFiles