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
Solved

arraylist images from directory sorting by filename

Posted on 2009-03-31
7
1,038 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET (2008) - Refactoring Question 2 23
Cant save 3D 4 22
Please explain the parts of these 2 LINQ expressions 3 53
linq, c# 8 22
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
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…

808 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