29

I have a ASP.Net (.net 3.5/c#) and I want to display a version number / build number and date. What is the best way in controling this and is it possible to auto incriment the numbers on build?

What is the standard for version numbers & build number?

Im using VS 2008 how would I get the data and assign to a string value so I can show in the footer of the webpage?

MartGriff
  • 2,821
  • 7
  • 38
  • 42

2 Answers2

39

If you're using a Web Application Project - you could do it like this...

Assembly web = Assembly.Load("App_Code");
AssemblyName webName = web.GetName();

string myVersion = webName.Version.ToString();

If you're using a Web Site project - nearly the same...

Assembly web = Assembly.GetExecutingAssembly();
AssemblyName webName = web.GetName();

string myVersion = webName.Version.ToString();
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
  • 1
    I'm trying to do this in a razor view (I know i shouldn't but its a shared layout) and it can't see the Assembly... – Worthy7 Sep 17 '16 at 06:42
  • @Worthy7, for a Razor view, you can set the Version in an Application variable in Application_Start() event (global.asax) like this: Application["Version"] = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();. Then retrieve it in the Razor view like this:

    version @Context.Application["Version"].ToString()

    – Bryan Williams Jul 23 '20 at 15:23
26

You can set the first two parts of the version number, and leave a wildcard for the compiler to autocomplete the last two parts, by editing the GlobalAssemblyInfo.cs like so:

[assembly:AssemblyFileVersion("1.0.*")]

It autocompletes the last two parts with a number of days since 1st Jan 2000, and the number of seconds since midnight. This may help with the second part of your query to display the date/time the version was built.

PaulG
  • 13,871
  • 9
  • 56
  • 78
  • 5
    cool! then you can get the version: System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() – Saber Nov 01 '11 at 16:05
  • the last part of the auto-completed version (the default revision number) is the number of seconds since midnight local time (without taking into account time zone adjustments for daylight saving time), divided by 2. Taken from here https://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute(v=vs.110).aspx – Zar Shardan Jan 01 '16 at 15:59