Small console application to change the version of a C# application
Content
If you want to update the assembly version of a C# program you can do it manually by setting the values in the AssemblyInfo.cs
. But when you have to do it manually the danger is great that you forget it. It's happened to me a lot. I've build my application and shipped it to the customers and then I noticed that I forgot to update the version number. So I had to go back to Visual Studio, update the version number and so on. Because of that I've created this small console application which will update the version number automatically. I've installed the VersionChanger as a pre-build event to my project and now I don't have to worry about the version number anymore.
The program offers you the possibility to specify some settings to customize the version number a little bit.
The following settings can currently be made
- The type of the version number (default: Days of the year, see Version type)
- The format of the version number (default: four places, see Version format)
- The version which should used
- The path of the assembly file
There are two ways how to set the settings:
- Command line parameters
- Configuration file
Note: You can combine the config. If a value is not specified in the command line arguments, it is taken from the config file.
Important: The command line arguments overwrite the config values! For example, when you specified the format with the value 2 in the config file but add the command line argument for the format with the value 1, the value from the command line (in this case 1) will be taken.
The tool can start with command line parameters. The following parameters are supported:
Nr. | Switch | Description | Example |
---|---|---|---|
1. | -p --path | Path of the desired assembly files | -p "C:\Repos\Application\Properties\AssemblyInfo.cs" "C:\Repos\TestApp\Properties\AssemblyInfo.cs" |
2. | -v | Version number | -v 1.2.4.12 |
3. | -f --format | Version format (see: Version format) | -f 2 |
4. | -t --type | Version type (see: Version type) | -t 1 |
Nr. | Switch | Description | Example |
---|---|---|---|
1. | -major | Major number | -major 1 |
2. | -minor | Minor number | -minor 2 |
3. | -build | Build number | -build 4 |
4. | -revision | Revision number | -revision 12 |
5. | -file | Assembly file path | -file "C:\Repos\Application\Properties\AssemblyInfo.cs |
6. | -version | Version number | -version 1.2.4.12 |
7. | -format | Version format (see: Version format) | -format 2 |
8. | -type | Version type (see: Version type) | -type 1 |
Note All parameters are optional and case intensitive.
Example
-
Assembly file and complete version
VersionChanger.exe -p "C:\Repos\Application\Properties\AssemblyInfo.cs" -v 1.2.4.12
-
Multiple files with format and type
VersionChanger.exe -p "C:\Repos\App\AssemblyInfo.cs" "C:\Repos\TestApp\AssemblyInfo.cs" -f 2 -t 1
The settings can also be made in a configuration file. The file has the following format:
{
"Version": {
"Major": 1,
"Minor": 2,
"Build": 3,
"Revision": 4
},
"AssemblyInfoFile": [
"D:\\Repositories\\MyApplication\\Properties\\AssemblyInfo.cs"
],
"Format": 2,
"VersionType": 1
}
Note: The name of the configuration file must be VersionChangerConfig.json and must be in the same folder as the application.
The following types are supported:
Id | Description | Example |
---|---|---|
1 | Days of the year (default) | 261 |
2 | Calendar week | 38 |
The following formats are supported:
Id | Description | Example |
---|---|---|
1 | Long format with four places (default) | 1.2.3.4 |
2 | Middle format with three places. | 1.2.3 |
3 | Short format with only two places. | 1.2 |
Note: If you provide the version number with the switch
version
it will be parsed automatically by theVersion.Parse
-Method (for more information click here).
If you don't specifie the version by yourself a new version number will be automatically generated. The format of the generated version is:
- Major: The last two numbers of the year (2019 > 19)
- Minor (see Version type)
- Build
- Revision: Minutes since midnight (e.G. 1146 > ~7pm) (only set when format 1 is selected. See Version format)
The Build number is calculated automatically. When the major and minor numbers are equal with the generated version the build number is increased by 1. When the numbers not equal the build number starts at 0.
Example (numbers equal)
- Current version:
19.261.0.1146
- Generated version:
19.261.0.1147
The major and the minor numbers are equal. New version: 19.261.1.1147
Example (numbers not equal)
- Current version:
19.260.4.1146
- Generated version:
19.261.0.1147
The major and the minor numbers are not equal. New version: 19.261.0.1147
If you want to install the VersionChanger as a pre-build event you have to do the following:
- Copy the executable of the VersionChanger to your desired project (I've copied it into the folder where the solution file is located).
- Right click on the project > Properties
- Select the tab Build Events
- Add the following:
$(ProjectDir)VersionChanger.exe
As soon as you compile the project now, the version number will change.
Build output
1>------ Rebuild All started: Project: Clock, Configuration: Release Any CPU ------
1> No version number was specified. Number is generated
1> Version numbers:
1> - Current version: 19.37.3.1250
1> - New version....: 19.37.4.1252
1> Version updated.
1> Clock -> D:\Repo\Clock\bin\Release\Clock.exe
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
If you don't want the the version number is changed with every build, you can add some conditions to the pre-build event. For example, the number should only be changed when you compile the project as release:
if $(ConfigurationName) == Release (
$(ProjectDir)VersionChanger.exe
)
Note The command will be interpreted line-by-line the same way as a DOS batch file, so it's important to place the opening "(" in the same line as the if statement!
Special thanks to Laszlo Pinter for his article about pre-/post-build events
But sometimes you want to build your project as release without changing the version number. For this case I have created the configuration PublishRelease for me.
A new configuration can be created under Build > Configuration Manager...:
Now you can add your configuration name to the pre-build event.
Example
-
Build with configuration Release:
1>------ Rebuild All started: Project: Clock, Configuration: Release Any CPU ------ 1> Clock -> D:\Repo\Clock\bin\Release\Clock.exe ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
-
Build with my custom configuration PublishRelease
1>------ Rebuild All started: Project: Clock, Configuration: PublishRelease Any CPU ------ 1> No version number was specified. Number is generated 1> Version numbers: 1> - Current version: 19.37.4.1252 1> - New version....: 19.37.5.1273 1> Version updated. 1> Clock -> D:\Repos\Clock\bin\PublishRelease\Clock.exe ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ========== ```
The .NET core version is still in development and differs from the .NET Framework application!