Show last file

I have one folder that contains all excel files. I want to show programmatically a recent excel file on the download page. I am using C # .net.plz help.

0


a source to share


1 answer


If by recent time you mean recently written, then you can use the following code to collect 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 FileInfo properties you might be interested in are LastAccessTime and CreationTime .




EDIT: Sorry, I didn't notice that you are using .NET 2.0. So, here is the equivalent code to find all excel files in a given directory and order them by their last recorded 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 mentioned uploading files in an ASP.NET application. That way, once you've got the list of files, you can show it to the user in a table so that he can find the file to download.

+1


a source







All Articles