tags:

views:

45

answers:

1

Hi, I am having one folder which contain all excel files.I want to show programmatically recent excel file in the page to download.I am using C#.net.plz help.

+1  A: 

If by recent you mean recently written to then you could use the following code to gather all excel files in a given directory and order them by last write time:

var files = from f in new DirectoryInfo(@"c:\some_directory").GetFiles("*.xls")
            orderby f.LastWriteTime descending
            select f;

foreach (var file in files)
{
    Console.WriteLine(file);
}

Other properties of FileInfo that might interest you are LastAccessTime and CreationTime.


EDIT: Sorry I didn't notice you were using .NET 2.0. So here is the equivalent code for finding all excel files in a given directory and order them by last write time:

List<FileInfo> files = new List<FileInfo>(new DirectoryInfo(@"c:\some_directory")
    .GetFiles("*.xls"));
files.Sort(delegate(FileInfo f1, FileInfo f2) 
{ 
    return f2.LastWriteTime.CompareTo(f1.LastWriteTime); 
});

In your question you mention downloading files in ASP.NET application. So once you have retrieved the list of files you can show it to the user in a table so that he can pick up the desired file to download.

Darin Dimitrov
code is not clear... were i have to mention this "orderby f.LastWriteTime descending" plz help
Xyz

related questions