Solved

arraylist images from directory sorting by filename

Posted on 2009-03-31
7
1,032 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 70
Code works but it's slow 28 70
EF5: Question about Metadata Artifact Processing 4 15
FTP file download using c# 3 22
Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

776 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