How to get different files from one directory with GetFiles()

Hello guys...
Is there a way to retrieve the names of all .bmp and .jpg files in one directory with only GetFiles() method....such that it puts all files into one array


Answer this question

How to get different files from one directory with GetFiles()

  • Jon Angel

    :) anyway I found the solution...sorry for bothering you

    DirectoryInfo dir = new DirectoryInfo("c:/images/");
    FileInfo[] imageFiles = dir.GetFiles("*.*");
    foreach (FileInfo file in imageFiles)
    {
    if (file.Extension.Equals(".bmp") || file.Extension.Equals(".jpg"))
    {
    listBox1.Items.Add(Convert.ToString(file.Name));
    }
    }

  • blabla21

    Sorry, i readed your question wrong. Thought you want to rename the files. For getting bmp and jpg files you can use:


    String[] files = Directory.GetFiles( directoryRoot, "*.bmp;*.jpg" );

    foreach( String file in files )
    {
    Console.WriteLine( "Found: " + Path.GetFileName(files) );
    }




  • Mosow

    chire wrote:
    :) anyway I found the solution...sorry for bothering you


    Thanks for the fast reply, but with your code you first retrieve all files and them filter them out. And compare them case-sencetive.


  • Ingar

    Here is a little self-explaining example:


    public void RenameAllBmpToJpg( string directoryRoot )
    {
    String[] files = Directory.GetFiles( directoryRoot, "*.bmp" );

    foreach( String file in files )
    {
    String destFileName = Path.ChangeExtension( file, ".jpg" );
    File.Move( file, destFileName );
    }
    }




  • Gyozo

    yeah, mine is a bit noobish...

    thanks a lot

    appreciate it!


  • How to get different files from one directory with GetFiles()