問題描述
獲取.Net Core 3.1 單文件可執行版本號 (Get .Net Core 3.1 single‑file executable version number)
我有一個已發佈為單個文件可執行文件的 .Net Core 應用程序。當我在 Visual Studio 中運行以下任何代碼時,我會得到預期的結果:
Assembly.GetAssembly(typeof(Installer)).GetName().Version.ToString();
Assembly.GetEntryAssembly().GetName().Version.ToString();
Assembly.GetExecutingAssembly().GetName().Version.ToString();
但是當上述代碼在已發布的應用程序(生產中)中運行時,它總是返回“0.0.0.0”
參考解法
方法 1:
Have you tried this? I created a new project and it seemed to work.
Assuming you're using Visual Studio 2019.
- Right‑click project ‑> Add ‑> New Item ‑> Assembly Information File
- You will then have a AssemblyInfo.cs file with the assembly version in
- You then need to ignore the generated assembly version so you need to add GenerateAssemblyInfo with false to your .csproj.
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>
方法 2:
The assembly version should be set in the properties of the project which you're compiling.
Right‑click project ‑> Properties ‑> Package: