Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
string[] fileEntries = Directory.GetFiles(Application.StartupPath + "/TrainedFaces/", "*.bmp", SearchOption.AllDirectories);

            Array.Sort(fileEntries);
            foreach (string fileName in (fileEntries))
                {
                   LoadFaces = System.IO.Path.GetFileName(fileName);
                   trainingImages.Add(new Image<Gray, byte>(fileName));
                 }


Output that I believe is wrong:

[0] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/1\\1.bmp"
[1] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/2\\2.bmp"
[2] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/2\\9.bmp"
[3] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/3\\3.bmp"
[4] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/4\\4.bmp"

What I expected is
[0] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFace/1\\1.bmp"
[1] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/2\\2.bmp"
[2] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/3\\3.bmp"
[3] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/4\\4.bmp"
[4] = "N:\\FaceRecProOV - Copy (2)\\FaceRecProOV\\bin\\Debug/TrainedFaces/4\\9.bmp"
Posted
Updated 5-Sep-12 4:45am
v2
Comments
Karthik. A 5-Sep-12 10:48am    
Is 9.bmp within folder "2" or "4" ? If its within "2", your output is correct. If you want to sort with just the file name, you may implement a custom sort using the IComparable interface. Add some information about your requirement and 1 of us can answer your questions.
[no name] 5-Sep-12 10:48am    
The first question I would ask is why you think that the path/filenames should magically change? e.g. TrainedFaces/2\\9.bmp, TrainedFaces/4\\9.bmp
harindup 5-Sep-12 10:53am    
To Karthik. A
So there is way, so can u please give me the code, my requirement is sort the string arry according 1.bmp ,2.bmp like wise

Hi there,

Aside from your data being inconsistent between your two lists (see 9.bmp. First list has 2\\, 2nd list has 4\\ - this affects the ordering significantly, as I will now explain.)

You have slightly misunderstood what the sort algorithm is doing. What it does is compare each letter from the start of each string to see which has a "greater" value. So (taking your first set of data), just before the last set of double backslahes it has a number. The sequence seen is:
1\\1
2\\2
2\\9
3\\3
4\\4

Which is correct. However, what you were looking for was the files to be sorted by the file name, not its full path. E.g.

1\\1
2\\2
3\\3
4\\4
2\\9

But as stated, the strings are sorted character by character from the start so the second result here is impossible. To do the sort of searching you are after you have one option really. Create a list of FileInfo objects and then sort by file name. Hopefully the following code works but it may not be bug free (untested):

C#
string[] fileEntries = Directory.GetFiles(Application.StartupPath + "/TrainedFaces/", "*.bmp", SearchOption.AllDirectories);
List<FileInfo> FileInfos = fileEntries.ToList().ConvertAll(x => new FileInfo(x)).ToList();
FileInfos = FileInfos.OrderBy(x => x.Name).ToList();
foreach (FileInfo AFileInfo in FileInfos)
{
    LoadFaces = AFileInfo.Name;
    trainingImages.Add(new Image<Gray, byte>(AFileInfo.FullName));
}


Hope this helps,
Ed
 
Share this answer
 
v6
Comments
harindup 5-Sep-12 11:08am    
List<fileinfo> FileInfos = fileEntries.ToList().ConvertAll(x => new FileInfo(x));

Giver error
Ed Nutting 5-Sep-12 11:10am    
I said it wouldn't be bug free... Make sure you have got "using System.Collections.Generic". Other than that you'll have to give me a more specific error message if you ecpect me to help.
Ed
Ed Nutting 5-Sep-12 11:12am    
Please note: I edited my solution since you copied that line of code. Please take another look at the line - you have not included the extra .ToList() at the end of the line of code.

Ed
var fileEntries = from f in Directory.GetFiles(Application.StartupPath + "/TrainedFaces/", "*.bmp", SearchOption.AllDirectories) let fi = new FileInfo(f) orderby fi.Name select fi.FullName;


Finally I found It my Self ,Thank For Peoples who are Help in this Problem
 
Share this answer
 

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