1

I want to have code that does the following:

foreach(File in Directory)
{
  test to see if the file is a jpeg
}

but am unfamiliar with how to read from files. How do I do this?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
TK421
  • 801
  • 5
  • 16
  • 24
  • 1
    http://stackoverflow.com/questions/12332451/c-sharp-list-all-files-and-directories-in-a-directory-subdirectories --- does this answer your quesiton? – crowder Jun 28 '13 at 22:21
  • If Jonathans answer isn't what your after, directory.getfiles().All(x => x.fileextension == jpg)); syntax maybe slightly off here – Sayse Jun 28 '13 at 22:35
  • Do you want to know if it's a valid jpeg file, or just that it has a jpeg extension? – Jim Mischel Jun 28 '13 at 23:06
  • I guess perhaps a better way of explaining it would be: `foreach(jpg in Directory){//Do something}` It should be fine just checking for a jpeg extension. – TK421 Jun 29 '13 at 01:01

3 Answers3

2

Why not use Directory.GetFiles() to get only the ones you want? This code will return all .jpg and .jpeg files.

Directory.GetFiles("Content/img/", ".jp?g");
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

If you're targetting .NET 4 Directory.EnumerateFiles may be more efficient, especially for larger directories. If not, you can replace EnumerateFiles with GetFiles below.

//add all the extensions you want to filter to this array
string[] ext = { "*.jpg", "*.jpeg", "*.jiff"  };
var fPaths = ext.SelectMany(e => Directory.EnumerateFiles(myDir, e, SearchOption.AllDirectories)).ToList();

Once you have a list of files with the correct extension, you can check if the file is actually a JPEG (as opposed to just being renamed .jpg) by using on of the two different methods mentioned in this answer. (From that post)

static bool HasJpegHeader(string filename)
{
    using (BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open)))
    {
        UInt16 soi = br.ReadUInt16();  // Start of Image (SOI) marker (FFD8)
        UInt16 jfif = br.ReadUInt16(); // JFIF marker (FFE0)

        return soi == 0xd8ff && jfif == 0xe0ff;
    }
}

Or a more accurate, but slower, method

static bool IsJpegImage(string filename)
{
    try
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile(filename);

        // Two image formats can be compared using the Equals method
        // See http://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat.aspx
        //
        return img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (OutOfMemoryException)
    {
        // Image.FromFile throws an OutOfMemoryException 
        // if the file does not have a valid image format or
        // GDI+ does not support the pixel format of the file.
        //
        return false;
    }
}

If there's a chance that you have JPEG files that don't have the correct extension then you'll have to loop through all files within the directories (use *.* as the filter) and perform one of the two methods above on them.

Community
  • 1
  • 1
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • After much testing it looks like yours is the answer that I needed. Thanks for your time and effort in this. Here is what I ended up with: `var fPaths = ext.SelectMany(a => Directory.EnumerateFiles(filepath, a, SearchOption.AllDirectories)).ToList(); for (int i = 0; i < fPaths.Count; i++) { MessageBox.Show(fPaths[i]); }` – TK421 Jun 29 '13 at 12:48
1

If all you want to know is which files have jpeg extensions, I would do this:

HashSet<string> JpegExtensions = 
    new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
        { ".jpg", ".jpe", ".jpeg", ".jfi", ".jfif" }; // add others as necessary

foreach(var fname in Directory.EnumerateFiles(pathname))
{
    if (JpegExtensions.Contains(Path.GetExtension(fname))
    {
        Console.WriteLine(fname); // do something with the file
    }
}
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351