troubleshooting Question

List files in directory in order of date and type

Avatar of GY1680
GY1680 asked on
5 Comments1 Solution692 ViewsLast Modified:
I'm trying to create a list of files that selects the "newest" file of a given type for three different file types.

I'm currently listing all files in the directory without any problems.

I know I can use the FileInfo[] array to sort or order the files - but I'm not sure how to select the top 1 (as in SQL) to display in a table.

Any help would be appreciated.
The code I'm using now to get all files in the directory is as follows:

    DirectoryInfo di = new DirectoryInfo("C:\\Inetpub\\Files\\");
    FileInfo[] rgFiles = di.GetFiles("*.pdf");
    TableRow tr = new TableRow();
    TableCell td = new TableCell();
    TableCell td2 = new TableCell();
    TableCell td3 = new TableCell();

    foreach (FileInfo fi in rgFiles)
        string File = "<a href='Files/" + fi.Name + "'>" + fi.Name + "</a>";

        tr = new TableRow();
        td = new TableCell();
        td.BorderStyle = BorderStyle.Solid;
        td.BorderWidth = 1;
        td.Text = File;
        td2 = new TableCell();
        td2.BorderStyle = BorderStyle.Solid;
        td2.BorderWidth = 1;
        td2.Text = fi.CreationTime.ToString();
        td3 = new TableCell();
        td3.BorderStyle = BorderStyle.Solid;
        td3.BorderWidth = 1;
        td3.Text = fi.Length.ToString("#,#") + " bytes";
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros