-1

I would like to display the version as yyyy.mm.dd of the last build date to users, how can I accomplish this? relatively new to c#.

Thanks!

Lucien P
  • 75
  • 1
  • 11
  • Check this out: http://stackoverflow.com/questions/1600962/displaying-the-build-date You can format the day in that manner by using: date.ToString("yyyy.MM.dd") – Matthew Alltop May 03 '17 at 18:58
  • This question doesn't have much to do with ASP.NET MVC - the question would largely be the same if you were using WPF or a console project. – Dai May 03 '17 at 19:11
  • @Dai I think the OP wants to use MVC versioning with the build number: `api/05-04-2017/products`. However it doesn't make sense to use build number for API versioning. – degant May 03 '17 at 19:16

1 Answers1

0

Between HttpContext and System.IO, you can find the location and last modification date of any file within your website

HttpContext (aka Server) has a MapPath() method within it to figure out where you are, and then you can add in the relative location of a particular file.

    string AppRoot = HttpContext.Current.Server.MapPath(@"\");
    string AppDll = AppRoot + @"bin\MyWebApp.dll"; 

System.IO can then work with that location to get all of the properties about a particular file. In your context you are looking for the Last Modified Date, and here is a simple method to retrieve it:

    public static DateTime GetLastModDate(string FilePath) {
        DateTime retDateTime;

        if (File.Exists(FilePath)) { retDateTime = File.GetLastWriteTime(FilePath); } 
        else { retDateTime = new DateTime(0);  }

        return retDateTime;
    }

Combine these and you get

DateTime LastBuildDate = GetLastModDate(AppDll);
Mad Myche
  • 1,075
  • 1
  • 7
  • 15
  • Just saw this, I ended up going a similar route: I grabbed the write date of the project.dll from the bin folder and converted the datetime to yyyy.mm.dd. I used HttpRuntime.AppDomainAppPath to get the path – Lucien P May 03 '17 at 20:39
  • I've used this in the _AppStart_ routine and just set a variable within so that it does not have to keep getting called. – Mad Myche May 03 '17 at 20:51
  • Ah that's a clever idea, thanks! – Lucien P May 03 '17 at 21:32