tags:

views:

49

answers:

1

I'm creating simple image viewer, but I want to make a sorting of pictures by LastTimeWrite. I have an OpenFileDialog to load pics, when I choose the pic in the folder then ProcessDirectory() is called.

private void ProcessDirectory()
        {
            FileTypes = new ArrayList();
            FileTypes.Add("*.JPG");
            FileTypes.Add("*.JPEG");
            FileTypes.Add("*.GIF");
            FileTypes.Add("*.BMP");
            FileTypes.Add("*.PNG");
            FileTypes.Add("*.TIF");
            FileTypes.Add("*.TIFF");

            string[] szFiles;
            FileArray = new ArrayList();

            foreach (string szType in FileTypes)
            {
                szFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), szType);
                if (szFiles.Length > 0)
                    FileArray.AddRange(szFiles);
                //switch (SortImg)
                //{
                //    case ("Asc"):
                //        FileArray.Sort();
                //        break;
                //    case("Date"):
                string[] MyString = new string[szFiles.Length];      
                        DateTime[] creationTimes = new DateTime[szFiles.Length];
                        for (int i = 0; i < szFiles.Length; i++)
                        {
                            creationTimes[i] = new FileInfo(szFiles[i]).LastWriteTime;
                            //creationTimes[i].ToString("yyyy-MM-dd HH:mm tt");
                        }

                        for (int i = 0; i < szFiles.Length; i++)
                            MyString[i] = Convert.ToString(creationTimes[i].ToString("yyyy-MM-dd HH:mm tt"));

                FileArray.Sort();
                        listBox1.Items.AddRange(MyString);
                        FileArray.AddRange(MyString);
...
                 }
+1  A: 

I would use a slightly different approach (this requires C# 3):

List<FileInfo> files = new List<FileInfo>();
List<string> fileTypes = new List<string>()
 {
     "*.jpg",
     "*.jpeg",
     "*.gif",
     "*.bmp",
     "*.png",
     "*.tif",
     "*.tiff"
 };

DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
foreach (string fileType in fileTypes)
{
    files.AddRange(dir.GetFiles(fileType));
}

var sortedFiles = files.OrderBy(f => f.LastWriteTime);

foreach (FileInfo file in sortedFiles)
{
    // do something with the FileInfo
}

If you are using C# 2 (so you don't have access to lambdas and Linq), it could look like this instead:

Directory.SetCurrentDirectory(@"C:\23055329\files\desktop wallpaper");
List<FileInfo> files = new List<FileInfo>();
List<string> fileTypes = new List<string>()
{
    "*.jpg",
    "*.jpeg",
    "*.gif",
    "*.bmp",
    "*.png",
    "*.tif",
    "*.tiff"
};

DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
foreach (string fileType in fileTypes)
{
    files.AddRange(dir.GetFiles(fileType));
}

files.Sort(new Comparison<FileInfo>(delegate(FileInfo x, FileInfo y)
{
    return x.LastWriteTime.CompareTo(y.LastWriteTime);
}));

foreach (FileInfo file in files)
{
    // do something with the FileInfo
    Console.WriteLine(file.ToString());
}
Fredrik Mörk
Thanks so much for your answering, Fredrik. Your examples work very well, but I don't see how to show sorted images, I can show only their FielInfo dates in string formats. I think there should be another array for sorted files which will be shown in pictureBox, but how to fill this new array with your array 'files' filled with sorted FileInfos. I'm too stupid for this. Do you have any idea? Thank you.
throbbing salami
If you want to instead show the images, you can se the `FromFile` method on the `Image` class to load the file and then assign the image to the `Image` property of a `PictureBox`. You will of course need one `PictureBox` for each image.
Fredrik Mörk