[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

DirectoryInfo sort by LastWriteTime

Posted on 2010-03-23
5
Medium Priority
?
1,292 Views
Last Modified: 2013-11-27
I am using DirectoryInfo to populate a Repeater control.  I have found an example of how to sort a DataGrid but not a Repeater.  

How do I sort a Repeater on LastWriteTime whose data comes from DirectoryInfo?
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
   Dim DiectorySupplements As New DirectoryInfo(Server.MapPath("archives/supplements"))
   Supplements.DataSource = DiectorySupplements.GetFiles("*.pdf")
   Supplements.DataBind()
End Sub


<asp:Repeater ID="Supplements" runat="server">
  <ItemTemplate>
    <asp:Label ID="Label1" runat="server"  Text='<%# Eval("Name") %>' />
  </ItemTemplate>
</asp:Repeater>

Open in new window

0
Comment
Question by:cgcmq
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:pschrama
ID: 28352218
Example using Linq (vb version), you need to add System.Linq namespace for this.

DiectorySupplements.GetFiles("*.pdf").OrderBy(Function(fi) fi.LastWriteTime)
0
 
LVL 10

Accepted Solution

by:
Alpesh Patel earned 2000 total points
ID: 28352307
Imports System.IO

Public Class clsCompareFileInfo

    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare

        Dim File1 As FileInfo

        Dim File2 As FileInfo

 

        File1 = DirectCast(x, FileInfo)

        File2 = DirectCast(y, FileInfo)

 

        Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)

    End Function

End Class

 

Then to use it…

 

Dim dirinfo As DirectoryInfo

Dim allFiles() As FileInfo

 

dirinfo = New DirectoryInfo(sSelPath)

allFiles = dirinfo.GetFiles("*.xml")

Array.Sort(allFiles, New clsCompareFileInfo)

For Each fl As FileInfo In allFiles

    MessageBox(fl.FullName.ToString())

Next

 
REF:http://geekswithblogs.net/ntsmith/archive/2006/08/17/88250.aspx
0
 

Author Comment

by:cgcmq
ID: 28357600
I edited my code as follows:

<%@ Import Namespace="System.Linq" %>

Dim DiectorySupplements As New DirectoryInfo(Server.MapPath("archives/supplements"))
Supplements.DataSource = DiectorySupplements.GetFiles("*.pdf").OrderBy(Function(fi) fi.LastWriteTime)
Supplements.DataBind()

Which returns the following error:
Compiler Error Message: BC30456: 'OrderBy' is not a member of 'System.Array'.
0
 

Author Closing Comment

by:cgcmq
ID: 31706056
Worked like a charm.  I altered one line in order to sort the files in descending order:

Compare = DateTime.Compare(File2.LastWriteTime, File1.LastWriteTime)

Cheers!
0
 
LVL 5

Expert Comment

by:pschrama
ID: 28360791
You need .NET Framework 3.5 to use the System.Linq namespace.

Otherwise you can use the IComparer approach, or adapt this example:


protected void Page_Load(object sender, EventArgs e)
      {
         DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Temp");

         FileInfo[] fileInfoArray = dirInfo.GetFiles("*.*");
         Array.Sort(fileInfoArray, CompareFiles);

         foreach (FileInfo fi in fileInfoArray)
         {
            Response.Write(fi.FullName + "<br />");
         }
      }

      private int CompareFiles(FileInfo fi1, FileInfo fi2)
      {
         return fi1.LastWriteTime.CompareTo(fi2.LastWriteTime);
      }

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…

612 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