简体   繁体   中英

Get .Net Core 3.1 single-file executable version number

I have a .Net Core application that has been published as a single file executable. When I run any of the following code from within Visual Studio, I get the expected result:

Assembly.GetAssembly(typeof(Installer)).GetName().Version.ToString();
Assembly.GetEntryAssembly().GetName().Version.ToString();
Assembly.GetExecutingAssembly().GetName().Version.ToString();

But when the above code is run within the published application (in production) it always returns "0.0.0.0"

Have you tried this? I created a new project and it seemed to work.

Assuming you're using Visual Studio 2019.

  1. Right-click project -> Add -> New Item -> Assembly Information File
  2. You will then have a AssemblyInfo.cs file with the assembly version in
  3. You then need to ignore the generated assembly version so you need to add GenerateAssemblyInfo with false to your .csproj.
  4. Right-click project -> Edit Project File -> Add GenerateAssemblyInfo element like below:

     <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <GenerateAssemblyInfo>false</GenerateAssemblyInfo> </PropertyGroup> </Project>

The assembly version should be set in the properties of the project which you're compiling.

Right-click project -> Properties -> Package:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM