# How to have an auto incrementing version number (Visual Studio)?

• ### Question

• User-501297529 posted

I currently hard code a version number using a label but I want to be able to displayed the version number by auto incrementing it instead of a hard code text. How do I do that?

Here is the current code for the label

   <asp:Label runat="server" ID="lblBuildNumber" ForeColor="White" Text="Build: 1.3.027" />

Thursday, September 27, 2018 7:25 PM

• User-501297529 posted

The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).

It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)

Or for now just include manually incrementing the build number as part of a post deployment procedure ?

First I appreciate the feedback.  I also want to state that this is not my idea for incrementing. The person that wants it done this way is a client that I'm working for. If it were up to me I would just use the default method of 1.2.* in the assembly version and go from there. This has to be a custom build so starting with build version 1.4.001 and then each build increments that last number so the next would be 1.4.002 and so on.

Based off of what mgehard said the console app has to be invoked in the post build event. I don't know much about it at all.  I don't know specifically what code/syntax goes there.  Does this make sense?

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, October 24, 2018 2:40 PM

### All replies

• User475983607 posted

Use reflection to get the build version.

https://stackoverflow.com/questions/909555/how-can-i-get-the-assembly-file-version

Thursday, September 27, 2018 7:38 PM
• User-501297529 posted

Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.

Thursday, September 27, 2018 8:15 PM
• User475983607 posted

#### bootzilla

Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.

I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.

protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
    <div class="row">
<div class="col-md-12" style="background-color:gray;">
<asp:Label runat="server" ID="lblBuildNumber" ForeColor="White" Text="" />
</div>
</div>

If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version.  If you are using the built in VS versioning then the above code will work.

Thursday, September 27, 2018 8:44 PM
• User-501297529 posted

#### bootzilla

Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.

I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.

protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
    <div class="row">
<div class="col-md-12" style="background-color:gray;">
<asp:Label runat="server" ID="lblBuildNumber" ForeColor="White" Text="" />
</div>
</div>

If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version.  If you are using the built in VS versioning then the above code will work.

I want to be able to auto version everytime I deploy to my production web server. Will the above code work for that?

Wednesday, October 3, 2018 3:55 PM
• User475983607 posted

I want to be able to auto version everytime I deploy to my production web server. Will the above code work for that?

The code shown displays the assembly version found in project properties.  Right click the project and select properties.  In the Application tab select the "Assembly Information" button.   Here you can set the Assembly version.  Insert an asterisk to auto-increment.

https://dailydotnettips.com/specifying-assembly-information-in-visual-studio/

https://blog.jsinh.in/how-to-auto-increment-version-number-of-assembly-in-dotnet-csharp/#.W7T5H2hKhaQ

There are VS plugins to make this a bit easier but you'll need to read the docs to make sure the plugin is a good fit.

https://marketplace.visualstudio.com/items?itemName=PrecisionInfinity.AutomaticVersions

Wednesday, October 3, 2018 5:20 PM
• User-501297529 posted

#### bootzilla

Based off of that example, how do I get the text label that displays the version number to automatically increment. The label is on a site master page.

I might have wrongly assumed the project properties are set to auto increment on each build and all you want to do is read the current build version.

protected void Page_Load(object sender, EventArgs e)
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
lblBuildNumber.Text = version.ToString();
}
    <div class="row">
<div class="col-md-12" style="background-color:gray;">
<asp:Label runat="server" ID="lblBuildNumber" ForeColor="White" Text="" />
</div>
</div>

If you have a custom versioning scheme then, it's up to you to increment the version according to your business requirements, store the version, and fetch the version.  If you are using the built in VS versioning then the above code will work.

So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label) or the other (set in Assembly.cs?

Also the current build number is 1.3.024 so how do I  increment from there.

Wednesday, October 10, 2018 4:09 AM
• User475983607 posted

#### bootzilla

So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label) or the other (set in Assembly.cs?

Again, the posted code takes advantage of the standard versioning scheme built into Visual Studio.   .NET also has a version type.  I would use the built in feature rather than building a custom versioning system.  Try going through the posted links to learn how versioning works in .NET

If you must use a custom build version system then it is up to you to craft a design.  One idea is to use an XML or JSON  file to persist the current version.  Craft a command line utility to increment the version by deserializing the file contents in to an object, increment the version according your your business logic, serialize the object, and save the file.  A database would also work.

If you use the Version type in .Net then craft the Version.json file like so.

{"Major":1,"Minor":3,"Build":24,"Revision":0,"MajorRevision":0,"MinorRevision":0}

This console app reads the file, increments the build, then saves the file.

        static void Main(string[] args)
{
Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

//Create a new Version and increment
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);

Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0'));

// serialize JSON directly to a file
using (StreamWriter file = File.CreateText("Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, newVersion);
}

}

The output

Current Version:        1.3.028
Incremented Version:    1.3.029
Press any key to continue . . .

Then in your web application simply read the file or database table and fetch the version when the application loads.  Use the sample code above or update it to suit your needs.

#### bootzilla

Also the current build number is 1.3.024 so how do I  increment from there.

I'm not sure how your build increment logic works but the code above should be enough for you to implement this logic on your own.

I used NewtonSoft to serialize the JSON.  NewtonSoft is available from NuGet.

https://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm#!

https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm

https://www.nuget.org/packages/Newtonsoft.Json/

Wednesday, October 10, 2018 4:22 PM
• User753101303 posted

Hi,

VS should do that automatically. According to https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assemblyversionattribute?redirectedfrom=MSDN&view=netframework-4.7.2 :

1.1.1.* should use the number of seconds since midnight.

1.1.. would use the would increment the 3rd component on each day (I assume this is the nuumber of days since a base date ?)

If I remember you had a difference between the C# compiler and the VB compiler in how those numbers are handled. Not sure if still true.

Wednesday, October 10, 2018 4:38 PM
• User-501297529 posted

Hi,

VS should do that automatically. According to https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assemblyversionattribute?redirectedfrom=MSDN&view=netframework-4.7.2 :

1.1.1.* should use the number of seconds since midnight.

1.1.. would use the would increment the 3rd component on each day (I assume this is the nuumber of days since a base date ?)

If I remember you had a difference between the C# compiler and the VB compiler in how those numbers are handled. Not sure if still true.

No this is all in C#, no VB.

As for the versioning number of seconds since midnight, I'm not sure what you mean there. The versioning number should increment during each build or publish.

Wednesday, October 10, 2018 6:04 PM
• User-501297529 posted

#### bootzilla

So I've added the label and set the label equal to the Assembly in the Page Load and setting the asterisk in the Assembly.cs. By doing it like this will this automatically show a new version every time the website is published or do I need to to do one (label) or the other (set in Assembly.cs?

Again, the posted code takes advantage of the standard versioning scheme built into Visual Studio.   .NET also has a version type.  I would use the built in feature rather than building a custom versioning system.  Try going through the posted links to learn how versioning works in .NET

If you must use a custom build version system then it is up to you to craft a design.  One idea is to use an XML or JSON  file to persist the current version.  Craft a command line utility to increment the version by deserializing the file contents in to an object, increment the version according your your business logic, serialize the object, and save the file.  A database would also work.

If you use the Version type in .Net then craft the Version.json file like so.

{"Major":1,"Minor":3,"Build":24,"Revision":0,"MajorRevision":0,"MinorRevision":0}

This console app reads the file, increments the build, then saves the file.

        static void Main(string[] args)
{
Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

//Create a new Version and increment
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);

Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0'));

// serialize JSON directly to a file
using (StreamWriter file = File.CreateText("Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, newVersion);
}

}

The output

Current Version:        1.3.028
Incremented Version:    1.3.029
Press any key to continue . . .

Then in your web application simply read the file or database table and fetch the version when the application loads.  Use the sample code above or update it to suit your needs.

#### bootzilla

Also the current build number is 1.3.024 so how do I  increment from there.

I'm not sure how your build increment logic works but the code above should be enough for you to implement this logic on your own.

I used NewtonSoft to serialize the JSON.  NewtonSoft is available from NuGet.

https://www.newtonsoft.com/json/help/html/SerializeWithJsonSerializerToFile.htm#!

https://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm

https://www.nuget.org/packages/Newtonsoft.Json/

I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?

Wednesday, October 10, 2018 6:06 PM
• User475983607 posted

I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?

All  the code does is fetch and write to a file.  Can you explain why, given the source code, you are unable to read the file in a web app and write the value to a server control's Text property?  Have you tried?

Wednesday, October 10, 2018 6:10 PM
• User-501297529 posted

#### bootzilla

I appreciate this but this may not be exactly what I'm looking for. The number should be displayed on a login page, so according to this type of coding where am I going to show the version number?

All  the code does is fetch and write to a file.  Can you explain why, given the source code, you are unable to read the file in a web app and write the value to a server control's Text property?  Have you tried?

The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.

Wednesday, October 10, 2018 6:18 PM
• User475983607 posted

The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.

I showed you two different ways with source code and documentation to solve this problem.  One using standard Visual Studio and a custom version where the version is persisted in a file.  I'm not sure what else I can do for you.

If you go with the custom solution above and place the version file in the application root, then this code will write the version to the page.

Version.json file

{"Major":1,"Minor":3,"Build":29,"Revision":0,"MajorRevision":0,"MinorRevision":0}

Global.asax

    public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}
}

Master page

            <footer>
<p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application <span><%=Application["Version"] %></span></p>
</footer>

All you have to do is run the console app to update the version file.  Remember to set the path.  You can kick off the console app by entering the path to the console app in the project properties -> Build Events.  The console app will run each time there is a build which will update the Version.json file.

https://msdn.microsoft.com/en-us/library/h7dhf0ty.aspx?f=255&MSPPError=-2147217396

Or you can use the standard Visual Studio versioning as recommend above.

Wednesday, October 10, 2018 6:33 PM
• User-501297529 posted

#### bootzilla

The current version number is hardcoded on a SiteMaster page, which is essentially a login page, as I show in my original post in this thread. That version number is 1.3.024 currently. I can even start at 1.4.000 or whatever at this point. Going forward I don't want it to be hardcoded and to automatically increment every time it is published to a production server. It needs to be displayed on the login page of the website. That's the reason why.

I showed you two different ways with source code and documentation to solve this problem.  One using standard Visual Studio and a custom version where the version is persisted in a file.  I'm not sure what else I can do for you.

If you go with the custom solution above and place the version file in the application root, then this code will write the version to the page.

Version.json file

{"Major":1,"Minor":3,"Build":29,"Revision":0,"MajorRevision":0,"MinorRevision":0}

Global.asax

    public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}
}

Master page

            <footer>
<p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application <span><%=Application["Version"] %></span></p>
</footer>

All you have to do is run the console app to update the version file.  Remember to set the path.  You can kick off the console app by entering the path to the console app in the project properties -> Build Events.  The console app will run each time there is a build which will update the Version.json file.

https://msdn.microsoft.com/en-us/library/h7dhf0ty.aspx?f=255&MSPPError=-2147217396

Or you can use the standard Visual Studio versioning as recommend above.

Got a question about the Global.asax file. I get 'The name 'RouteTable' does not exist in the current context when I add that code to the file. I also get the same for RouteConfig , BundleConfig, BundleTable, JsonSerializer. What do I need to do to fix those errors?

This is what my code looks like currently when I add the code you provided to it:

 public class Global : System.Web.HttpApplication
{
public static IConverter PDFConverter;

protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));

// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}

Do I just generate properties for all of those that have that error?

Tuesday, October 23, 2018 3:11 PM
• User475983607 posted

I'm not sure what you are doing.... Post your original Global.asax file and I'll the paste the code into the file to reduce the errors.  Or just paste the highlighted code at the end of the Application_Start handler.

Tuesday, October 23, 2018 3:19 PM
• User-501297529 posted

All I did was paste the code you provided into the Application_Start handler. This was the code in the handler before I pasted.

 protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));
}

This is after(highlighted):

 protected void Application_Start(object sender, EventArgs e)
{
PDFConverter =
new RemotingToolset<PdfToolset>(
new WinAnyCPUEmbeddedDeployment(
new TempFolderDeployment())));

// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

Application["Version"] = string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));
}

That's where I get the errors.

Tuesday, October 23, 2018 3:28 PM
• User753101303 posted

Seems you are missing at the top of your file :

using System.Web.Routing;

I tried my previous suggestion which seems to work out of the box ie using [assembly: AssemblyVersion("1.0.*")] allows the CS compiler to assign a build/revision to each new build (though this is not an increment, it uses what I told earlier to generate unique numbers each time you create a new DLL without having to care about the previous version number).

Tuesday, October 23, 2018 3:51 PM
• User-501297529 posted

Seems you are missing at the top of your file :

using System.Web.Routing;

I tried my previous suggestion which seems to work out of the box ie using [assembly: AssemblyVersion("1.0.*")] allows the CS compiler to assign a build/revision to each new build (though this is not an increment, it uses what I told earlier to generate unique numbers each time you create a new DLL without having to care about the previous version number).

using System.Web.Routing;

This fixes RouteTable but I still get errors for RouteConfig, BundleConfig, BundleTable, and JsonSerializer. All the same error  'The name 'RouteConfig' does not exist in current context.

And yes I agree that using [assembly: AssemblyVersion("1.0.*")] will generate some random build number like 1.4.6865.30445 which is not what I want.

Tuesday, October 23, 2018 3:57 PM
• User475983607 posted

#### bootzilla

All I did was paste the code you provided into the Application_Start handler. This was the code in the handler before I pasted.

Yeah, you posted the non-highlighted code.  Only post the highlighted code.  That's why I highlighted it...

Remove this...

           // Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

Tuesday, October 23, 2018 5:01 PM
• User753101303 posted

More generally you should have a "light bulb" on this kind of errors and you should then see suggestions such as using the full name ie someNameSpace.RouteConfig or to add a using NameSpace; directive at the top of your file. See https://dirkstrauss.com/visual-studio-2015-light-bulb/

It could be also that a namespace declaration is missing (AFAIK it is common to declare MvcApplication and RouteConfig in the same namespace in which case you won't have this issue).

Tuesday, October 23, 2018 5:41 PM
• User-501297529 posted

More generally you should have a "light bulb" on this kind of errors and you should then see suggestions such as using the full name ie someNameSpace.RouteConfig or to add a using NameSpace; directive at the top of your file. See https://dirkstrauss.com/visual-studio-2015-light-bulb/

It could be also that a namespace declaration is missing (AFAIK it is common to declare MvcApplication and RouteConfig in the same namespace in which case you won't have this issue).

For the JsonSerializer error I get 'the type or namespace 'JsonSerializer' could not be found' and the light bulb suggestion is to generate a class for that in a new file. That doesn't sound right.

Tuesday, October 23, 2018 5:46 PM
• User753101303 posted

If unfamiliar with nuget see https://docs.microsoft.com/en-us/nuget/tools/package-manager-ui (uses the Visual Studio UI) or https://docs.microsoft.com/en-us/nuget/tools/package-manager-console (allows to launch nuget commands from a  Visual Studio "console" window).

Tuesday, October 23, 2018 6:10 PM
• User-501297529 posted

#### PatriceSc

If unfamiliar with nuget see https://docs.microsoft.com/en-us/nuget/tools/package-manager-ui (uses the Visual Studio UI) or https://docs.microsoft.com/en-us/nuget/tools/package-manager-console (allows to launch nuget commands from a  Visual Studio "console" window).

Installed and it worked!

OK now I need to get the label or whatever on the master page to show the version number on the login page. Here is the code I have but it doesn't seem to work.

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="true" />

Tuesday, October 23, 2018 6:37 PM
• User475983607 posted

#### bootzilla

Installed and it worked!

OK now I need to get the label or whatever on the master page to show the version number. Here is the code I have but it doesn't seem to work.

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="tr

Use a span tag as recommended in the code above since an ASP label renders as a span anyway.

<span><%=Application["Version"] %></span>

If you want to use a server control then set the Text property in the Master Page code behind.

This markup is setting the "for" attribute

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="true" />

Tuesday, October 23, 2018 6:58 PM
• User-501297529 posted

#### bootzilla

Installed and it worked!

OK now I need to get the label or whatever on the master page to show the version number. Here is the code I have but it doesn't seem to work.

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="tr

Use a span tag as recommended in the code above since an ASP label renders as a span anyway.

<span><%=Application["Version"] %></span>

If you want to use a server control then set the Text property in the Master Page code behind.

This markup is setting the "for" attribute

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="true" />

Using that span I don't get anything to show. I'm not using it in the footer. This is what I have the label for the word 'Build' and then the span tag:

 <asp:Label runat="server" ID="Label1" ForeColor="White" Text="Build:" />&nbsp;
<span><%=Application["Version"] %></span>


How it looks on the page is 'Build: '

Using it this way I get ' server tags cannot contain <% %> constructs error

<asp:Label for =" <%=Application["Version"] %>" runat="server" ID="lblBuildNumber" ForeColor="White" Text="" Visible="true" />

Tuesday, October 23, 2018 7:09 PM
• User475983607 posted

You'll need to set a breakpoint and step through the code to make sure the application variable is set.

Did you copy the Version.json file to the application root?

using (StreamReader file = File.OpenText(Server.MapPath("Version.json")))

Tuesday, October 23, 2018 7:14 PM
• User-501297529 posted

#### mgebhard

You'll need to set a breakpoint and step through the code to make sure the application variable is set.

Did you copy the Version.json file to the application root?

using (StreamReader file = File.OpenText(Server.MapPath("Version.json")))

Not sure what you mean to make sure application variable is set. Isn't it already set on the global.asax page?

And yes it's on the project root folder, if that is what you mean.

Tuesday, October 23, 2018 7:18 PM
• User475983607 posted

Yes it's on the project root folder, if that is what you mean.

Maybe background color and font color are the same?

<span style="color:white"><%=Application["Version"] %></span>

Tuesday, October 23, 2018 7:28 PM
• User-501297529 posted

#### bootzilla

Yes it's on the project root folder, if that is what you mean.

Maybe background color and font color are the same?

<span style="color:white"><%=Application["Version"] %></span>

That was it. I see version number Build: 1.4.001

Now that everything seems to be set up.  When I build on that project the next version should show as 1.4.002, correct?

Tuesday, October 23, 2018 7:35 PM
• User475983607 posted

Now that everything seems to be set up.  When I build on that project the next version should show as 1.4.002, correct?

I will work if you followed the instructions outlined above.

1. Invoke the console app in the build event of the web project.
2. The console app is writing the file to the application root.
Tuesday, October 23, 2018 7:50 PM
• User-501297529 posted

#### bootzilla

Now that everything seems to be set up.  When I build on that project the next version should show as 1.4.002, correct?

I will work if you followed the instructions outlined above.

1. Invoke the console app in the build event of the web project.
2. The console app is writing the file to the application root.

Not sure exactly how to do this. I'm not good  with command lines at all.

So does this go in the pre-build event? If so do I call the path to the version.json file? and put  C:/ProjectFolder/version.js in the event command line event?

Tuesday, October 23, 2018 7:59 PM
• User475983607 posted

#### bootzilla

So does this go in the pre-build event?

I would put the path to the console app in the post_built event.  Keep in mind, this is not a polished feature but it should be more than enough to get you started.

The concept is simple though.  The post-build event kicks off the console app.  The console app updates the version.json file in the web app.

The parts that are missing are placing the console app in a known location and using configuration to write the file location if this is a team feature.  Also the solution increments the last value in the Version only.  You'll need to modify the console app to increment the other Version parts according to your business rules.

If so do I call the path to the version.json file? and put  C:/ProjectFolder/version.js in the event command line event?

No, the console app writes the file.  The post-build event uses the path to the Console app exe.  I placed the exe in the web project's bin folder.

Tuesday, October 23, 2018 8:20 PM
• User-501297529 posted

#### bootzilla

So does this go in the pre-build event?

I would put the path to the console app in the post_built event.  Keep in mind, this is not a polished feature but it should be more than enough to get you started.

The concept is simple though.  The post-build event kicks off the console app.  The console app updates the version.json file in the web app.

The parts that are missing are placing the console app in a known location and using configuration to write the file location if this is a team feature.  Also the solution increments the last value in the Version only.  You'll need to modify the console app to increment the other Version parts according to your business rules.

#### bootzilla

If so do I call the path to the version.json file? and put  C:/ProjectFolder/version.js in the event command line event?

No, the console app writes the file.  The post-build event uses the path to the Console app exe.  I placed the exe in the web project's bin folder.

OK I'm confused here. What goes in the post event? Is it C:/ProjectFolder/something.exe? What is this console app exe. Is that something that gets created during the build? I'm just trying to figure this out. You have to keep in mind I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.

Tuesday, October 23, 2018 8:26 PM
• User475983607 posted

What is this console app exe. Is that something that gets created during the build?

The console app idea was explained above.

https://forums.asp.net/post/6233357.aspx

I came up with this idea because you do not want to use the built-in VS feature and the versioning scheme is custom.

I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.

I'm not sure what I can do for you if you feel that way.  From my perspective, I've been more than specific but you ignore the advice, don't try the source code, or even read the posted links.  Case in point, the use of NewtonSoft to serialize JSON including the NuGet link.

It seems you want someone to craft an entire solution according to your requirements.  You have to put in some work as well.

Tuesday, October 23, 2018 8:49 PM
• User-501297529 posted

#### bootzilla

What is this console app exe. Is that something that gets created during the build?

The console app idea was explained above.

https://forums.asp.net/post/6233357.aspx

I came up with this idea because you do not want to use the built-in VS feature and the versioning scheme is custom.

#### bootzilla

I haven't done this before so just giving me bits and pieces isn't going to help. You will need to be more specific.

I'm not sure what I can do for you if you feel that way.  From my perspective, I've been more than specific but you ignore the advice, don't try the source code, or even read the posted links.  Case in point, the use of NewtonSoft to serialize JSON including the NuGet link.

It seems you want someone to craft an entire solution according to your requirements.  You have to put in some work as well.

Man you are incredibly difficult at times. I HAVENT DONE THIS BEFORE. What part of that do you not understand. You are going to have to break this down for me please. You didn't explain what exactly the console app is. Is it global.asax? Is it something else.  What goes in the build event specifically. I don't know that's why I'm asking. Telling me you are not sure what you can do for me is bs. YES YOU CAN. You are choosing not to for some reason. If you take the time to break down each part of what I need to do in the build events that would be a very helpful start.

And no I don't want someone to craft the entire solution.  That is complete bs as well. Where do you get these crazy ideas. This not even close to an entire solution. I need assistance on a very small piece that I have not done before. That's why I came here to ask for help. I don't know anything about build events and I'm asking for help in that direction. You seem to not understand this. I'm asking for it very cordially. You are not and have been very difficult throughout this process.

So I'm going to ask again what do I put in the build event SPECIFICALLY. I don't know what to put there since I haven't done this before.

Tuesday, October 23, 2018 8:53 PM
• User-501297529 posted

Can anyone assist me in finishing this please. I don't know much about post event builds and I need to know what to put in there and the person that pointed me in this direction doesn't seem to want to help anymore.

I would really appreciate someone assisting me with this.

Wednesday, October 24, 2018 1:50 PM
• User753101303 posted

The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).

It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)

Or for now just include manually incrementing the build number as part of a post deployment procedure ?

Wednesday, October 24, 2018 2:35 PM
• User-501297529 posted

The pre build event allows to launch an app each time you build the application. What runs would be a console app that you have to write and that would increment the build version (the sample above just use the AssemblyInfo.cs file).

It could still skip numbers as you may build several times before publishing the next version on the web site (which is likely why the default VS approach favors uniqueness and de-emphasize using consecutive numbers)

Or for now just include manually incrementing the build number as part of a post deployment procedure ?

First I appreciate the feedback.  I also want to state that this is not my idea for incrementing. The person that wants it done this way is a client that I'm working for. If it were up to me I would just use the default method of 1.2.* in the assembly version and go from there. This has to be a custom build so starting with build version 1.4.001 and then each build increments that last number so the next would be 1.4.002 and so on.

Based off of what mgehard said the console app has to be invoked in the post build event. I don't know much about it at all.  I don't know specifically what code/syntax goes there.  Does this make sense?

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, October 24, 2018 2:40 PM
• User475983607 posted

#### bootzilla

Can anyone assist me in finishing this please. I don't know much about post event builds and I need to know what to put in there and the person that pointed me in this direction doesn't seem to want to help anymore.

I would really appreciate someone assisting me with this.

Huh?  Well, the threatening PMs of harm don't help and, like many of us, have other stuff to do.

The following post has the a console app.

https://forums.asp.net/post/6233357.aspx

Create you're own console application in Visual Studio and copy the main() method to your new console app.   Be sure to add the NewtonSoft NuGet package.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;

namespace ConsoleCs
{
class Program
{

static void Main(string[] args)
{
Version version;
// deserialize JSON directly from a file
{
JsonSerializer serializer = new JsonSerializer();
version = (Version)serializer.Deserialize(file, typeof(Version));
}

Console.WriteLine("Current Version:\t{0}.{1}.{2}", version.Major, version.Minor, version.Build.ToString().PadLeft(3, '0'));

//Create a new Version and increment
Version newVersion = new Version(version.Major, version.Minor, version.Build + 1, version.Revision);

Console.WriteLine("Incremented Version:\t{0}.{1}.{2}", newVersion.Major, newVersion.Minor, newVersion.Build.ToString().PadLeft(3, '0'));

// serialize JSON directly to a file
using (StreamWriter file = File.CreateText(@"./Version.json"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, newVersion);
}

}
}
}


Build the app - do not run the app.  Copy the "Version.json" file to the console app's bin/debug folder.  You can find the bin folder by right clicking the project in solution explorer and selecting "Open folder in file explorer".  Then just open the bin/debug folder.  Run the Console app and it should update the Version.json file in the bin folder.

Update the following two lines of code by replacing "Version.json" with the fully qualified path to the Version.json file in your web application.

using (StreamReader file = File.OpenText(@"./Version.json"))

using (StreamWriter file = File.CreateText(@"./Version.json"))

It will look something like this... Note the "@".

using (StreamReader file = File.OpenText(@"C:\Path\to\the\Web\App\Version.json"))

Run the app again and it should update the Version.json file located in the web app.

Copy the console app (exe) to the web app's bin folder.  Open the console app's bin/debug folder.  Find the exe file that has the same name as your console app and copy the exe to your web app's bin folder.  Also copy the *.exe.config file.

Open the web app's properties and go to the Build Events.  Enter the exe file name; ie. MyConsoleApp.exe.

Now every time the project is build, the console app will run and update the Version.json File.

As PatriceSc suggested, this will update the Version every time the project is built.  Not when deployed.  But, you can trigger the console app whenever you like.

Lastly, this is not a polished production feature as it only increments the last number 1.0.XXX.  I'm pretty sure you still need to implement your Versioning business logic. You have all the source code so modify it to suit your needs.

Wednesday, October 24, 2018 3:03 PM
• User753101303 posted

It's basically just a command line and you can use \$(variable) values that are replaced before it runs so that you can transmit to this app parameters that depends on the current solution (such as the root folder for your solution etc...). It's likely rather on the pre build event.

If I had to do that I wonder if I wouldn't consider this to be rather a "deployment counter". For example I would have perhaps a table with the actual version and/or  DLL modification date (maybe not taking the time into account) to register in my db a new "deployment" each time a new DLL is first used.

Wednesday, October 24, 2018 3:29 PM