Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current published version in a .NET application?

Tags:

c#

.net

I want to be able to display the current version of a .NET application that I have deployed using the publish wizard. There is a nice option to automatically update the version number every time I publish my application.

I found another question (Automatically update version number) that had this to get the current version:

Assembly.GetExecutingAssembly().GetName().Version 

This gets you the version you set in the project properties, but not the version that is automatically incremented each time you publish.

like image 341
Ed Haber Avatar asked Aug 08 '09 12:08

Ed Haber


People also ask

How do I publish a .NET core 3.1 application?

Published your ASP.NET Core Web API as FDD So open Visual Studio and Go to File -> New -> Project. Select ASP.NET Core Web Application and click on Next. Give the proper name to your project and click on Create.

What is the difference between dotnet build and dotnet publish?

Build compiles the source code into a (hopefully) runnable application. Publish takes the results of the build, along with any needed third-party libraries and puts it somewhere for other people to run it.


1 Answers

You can use the following test

if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) {     return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion; } 

to avoid the exception (as detailed in this post).

Also, I don't think you can get the current publish version via Visual Studio debugging because accessing CurrentDeployment will throw an InvalidDeploymentException.

like image 100
Jason Avatar answered Oct 02 '22 16:10

Jason