Solved

arraylist images from directory sorting by filename

Posted on 2009-03-31
7
1,021 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
Comment Utility
What version C# you working with?
0
 

Author Comment

by:cay187
Comment Utility
.net 3.5
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:cay187
Comment Utility
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
Comment Utility
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
Comment Utility
i will try this today
0
 

Author Closing Comment

by:cay187
Comment Utility
That did it.  Thank you very much!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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 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 reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

771 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

10 Experts available now in Live!

Get 1:1 Help Now