report RRS feed

  • Question

  • User-1578974752 posted


    This folder is having more than 1 reports. how can i change this code so that it will pick the latest one.(by time).thnaks

    Tuesday, January 15, 2019 7:17 AM

All replies

  • User-1174608757 posted

    Hi shsu,

    According to your description, I suggest you to use foreach to traversing the whole folder so you could get the latest file based on each lastwritetime.

    Here is a demo, I hope it could help you.


     public partial class file : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)
                string Folder = @"D:\WebApplication1";
                var files = new DirectoryInfo(Folder).GetFiles("*.*");// get all files
                string latestfile = "";
                DateTime lastupdated = DateTime.MinValue;
                foreach (FileInfo file in files)
                    if (file.LastWriteTime > lastupdated)
                        lastupdated = file.LastWriteTime;
                        latestfile = file.Name;

    Folder shows as:

    Aspx shows as :

    Best Regards

    Wei Zhang

    Tuesday, January 15, 2019 8:52 AM
  • User503812343 posted

    try below code

    var directory = new DirectoryInfo("C:\\MyDirectory");
    var myFile = (from f in directory.GetFiles()
                 orderby f.LastWriteTime descending
                 select f).First();
    // or...
    var myFile = directory.GetFiles()
                 .OrderByDescending(f => f.LastWriteTime)

    Tuesday, January 15, 2019 6:16 PM