Solved

arraylist images from directory sorting by filename

Posted on 2009-03-31
7
1,026 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
  • 4
  • 3
7 Comments
 
LVL 85

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 85

Accepted Solution

by:
Mike Tomlinson earned 500 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now