Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a folder C:\Images\ contains 12 Images.I have use the below code to extract all the images from the folder and display in file list. But the images are not in the proper order.

DirectoryInfo dir = new DirectoryInfo((strImagePaths));
FileInfo[] file = dir.GetFiles();

Its displaying as Img1,img10,img11,img12,img2,img3,img4,img5,img6,img7,ing8,img9 by some kind of sorting

But i need to display in numerical order like img1,2,3,4,5,6,7,8,9,10,11,12

Thanks in advance
Posted
Comments
Renjith_R 13-Jan-15 10:21am    
string[] files = Directory.GetFiles(strImagePaths, "*.Jpeg*", SearchOption.AllDirectories);
And
List<string> Msilist = new List<string>();
string strfileName = string.Empty;
DirectoryInfo d = new DirectoryInfo(strImagePaths);// Folder Path
FileInfo[] Files = d.GetFiles("*.Jpeg"); //Getting msi files
foreach (FileInfo file1 in Files)
{
string strname = file1.Name;
Msilist.Add(strname);
}
will gives the same result Img1,img10,img11,img12,img2,img3,img4,img5,img6,img7,ing8,img9

You need a natural sort order. Try this solution:

http://www.interact-sw.co.uk/iangblog/2007/12/13/natural-sorting
 
Share this answer
 
v3
Here is an alternative solution using a generic collection
C#
// Create a sorted list where the key will be a string = adjusted filename, value = the original FileInfo
var sl = new System.Collections.Generic.SortedList<string, FileInfo>();
foreach (var f in file)
{
    // Get the name
    var jpgName = f.Name;
    // find the number
    var resultString = System.Text.RegularExpressions.Regex.Match(jpgName, @"\d+").Value;

    //Make sure we have a number
    int res;
    if(int.TryParse(resultString, out res))
    {
        // Convert the filename we will use as a key - you might need more zeroes if you have a lot of files
        jpgName = String.Format("{0}{1}", jpgName.Replace(resultString, ""), res.ToString("0000000"));
    }
    sl.Add(jpgName, f);
}

// Sorted list sl now contains the files in the natural sort order
 
Share this answer
 
Comments
Renjith_R 13-Jan-15 10:43am    
Thanks for your quick reply, but this also gives the same result
this also gives the same result
image1
image10
image11
image12
image2
image3
image4
image5
image6
image7
image8
image9

my expected output should be
image1
image2
image3
image4
image5
image6
image7
image8
image9
image10
image11
image12
CHill60 13-Jan-15 10:48am    
Nope - if you use
foreach (var i in sl)
Console.WriteLine(i.Value.FullName);
You will see that my code produces the list in the correct order
you can do as below
C#
List<string> Msilist = Directory.GetFiles(strImagePaths, "*.Jpeg")
                                     .Select(path => Path.GetFileNameWithoutExtension(path))
                                     .OrderBy(x => int.Parse(x.TrimStart("image".ToArray()))).ToList();

note that you need to order by number part of the file name
 
Share this answer
 
v2
Comments
CHill60 13-Jan-15 11:02am    
I quite liked the look of this but when I try it I get
'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
'string' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
DamithSL 13-Jan-15 11:22am    
CHill60, I have test my code with sample files using LINQPad and only issue was need to get the file name without extension because I only remove the first part of the file name.
CHill60 13-Jan-15 11:25am    
I just pasted it "as is" into Visual Studio 2010 Express (which is what I happened to have open). .net framework 4.0 client targeted.
DamithSL 13-Jan-15 11:37am    
have you add
using System.Linq;
CHill60 13-Jan-15 11:40am    
*groan* - Good call. I'd forgotten that I'd done a "Remove Unused Usings" earlier - so of course I wasn't getting the Linq extension methods :facepalm:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900