Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

arraylist images from directory sorting by filename

Posted on 2009-03-31
7
Medium Priority
?
1,066 Views
Last Modified: 2012-05-06
I am using c# with asp.net to fill an arraylist with images (jpg) that are in a directory.  i am using the filesystem to upload images to a directory and i am renaming them with a date_time stamp as the file name.  ie  
03202009_10_22_13_pm.jpg
03122009_11_29_05_am.jpg
03312009_1_15_45_pm.jpg
how can i display the arraylist in a datalist sorting by FILE NAME?


if (Directory.Exists(Server.MapPath(strPath)))
        {
            DirectoryInfo dirInfoImages = new DirectoryInfo(Server.MapPath(strPath));
            ArrayList listImages = new ArrayList();
            listImages.AddRange(dirInfoImages.GetFiles("*.jpg"));
            listImages.AddRange(dirInfoImages.GetFiles("*.jpeg"));                        
            DL_Images.DataSource = listImages;
            DL_Images.DataBind();            
        }

Open in new window

0
Comment
Question by:cay187
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24032519
What version C# you working with?
0
 

Author Comment

by:cay187
ID: 24032538
.net 3.5
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 24033038
Try something like this:

        ...
            if (Directory.Exists(Server.MapPath(strPath)))
            {
                DirectoryInfo dirInfoImages = new DirectoryInfo(Server.MapPath(strPath));
                List<FileInfo> listImages = new List<FileInfo>();
                listImages.AddRange(dirInfoImages.GetFiles("*.jpg"));
                listImages.AddRange(dirInfoImages.GetFiles("*.jpeg"));
                listImages.Sort(CompareFileInfos);
                DL_Images.DataSource = listImages;
                DL_Images.DataBind();
            }

With the helper method:

        private int CompareFileInfos(FileInfo fiA, FileInfo fiB)
        {
            return fiA.Name.CompareTo(fiB.Name);
        }

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cay187
ID: 24035542
List<FileInfo> listImages = new List<FileInfo>();
you are way more advanced at arrays than i am.  you suggest i replace
ArrayList listImages = new ArrayList();
with
List<FileInfo> listImages = new List<FileInfo>();?
what is<FileInfo>?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24035704
The List is basically a TYPE-SAFE, dynamic array.  The <FileInfo> part is telling it what Type of data will be in the list.
See Generic List: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

An ArrayList is a dynamic list that accepts anything because it takes Object.  So you were actually placing FileInfo instances into your ArrayList with your old code because DirectoryInfo.GetFiles() returns an Array of FileInfo.
See DirectoryInfo.GetFiles(): http://msdn.microsoft.com/en-us/library/8he88b63.aspx

0
 

Author Comment

by:cay187
ID: 24059082
i will try this today
0
 

Author Closing Comment

by:cay187
ID: 31565001
That did it.  Thank you very much!!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question