Logo

Programming-Idioms

Construct a list L that contains all filenames that have the extension ".jpg" , ".jpeg" or ".png" in directory D and all its subdirectories.
New implementation

Type ahead, or select one

Explain stuff

To emphasize a name: _x → x

Please be fair if you are using someone's work

You agree to publish under the CC-BY-SA License

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string D = @"C:\The\Search\Path";

// Assumes that the file system is case insensitive
HashSet<string> exts = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase) { "jpg", "jpeg", "png" };
IEnumerable<string> L =
Directory
    .EnumerateFiles(D, "*", SearchOption.AllDirectories)
    .Where(currentFile => exts.Contains(Path.GetExtension(currentFile)));

Enumerating all files under the given directory select any file whose extension exists in the HashSet (case insensitive using the current culture). If you want this to be case sensitive you can remove the comparer.