简体   繁体   中英

Visual Studio C# App Version in .NET CORE 6 (used to do in in .Net Framework 4.6.2)

I have searched every Google search combinations I know, and I have tried multiple ways to accomplish the following:

In.Net Framework 4.6.2, I could use:

       ApplicationDeployment currentAppDeploy = ApplicationDeployment.CurrentDeployment;

       string version = "v" + currentAppDeploy.CurrentVersion.Major + "." +
            currentAppDeploy.CurrentVersion.Minor + "." +
            currentAppDeploy.CurrentVersion.Revision;

       return version;

How can I get the version from Visual studio of a .NET CORE 6 app?

I have searched every Google search combination I know, and I have tried multiple ways to accomplish it.

We can define the version numbers for AssemblyVersion, FileVersion and Version in the project file (.csproj).

 <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <AssemblyVersion>1.1.1</AssemblyVersion>
    <FileVersion>2.2.2</FileVersion>
    <Version>3.3.3-xyz</Version>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

AssemblyVersion
1. GetType().Assembly.GetName().Version.ToString()
2. Assembly.GetEntryAssembly().GetName().Version

Both two methods will return 1.1.1

FileVersion

Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version
It will return 2.2.2

Version

Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion
It will return 3.3.3





using System;
using System.Reflection;

    class Program
    {
        static void Main(string[] args)
        {
       
            Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
                              $"{Assembly.GetEntryAssembly().GetName().Version}");

            Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
                              $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");

            Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
                              $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute> ().InformationalVersion}");

            Console.ReadKey();
        }
    }

在此处输入图像描述

So far, this cannot be done in .NET 6, but there is a ussue on github where this revision is discussed and the developers promise to release it together with support.NET8 https://github.com/dotnet/deployment-tools/issues/27

Thank you for the responses. Much appreciated. Based on an answer provided by @lomobit, I read through the GitHub posts and found a class I was able to include in my code. Here is the information and an example:

GitHub Post: https://github.com/dotnet/deployment-tools/issues/27

Class to include: https://github.com/dotnet/deployment-tools/blob/dfb0a98c68da39fc9cd1d82e200a19badf4e7112/Documentation/dotnet-mage/ApplicationDeployment.cs (I currently embedded it in another Helper class, but will likely create a class of its own.)

            if (Helper.ApplicationDeployment.IsNetworkDeployed)
            {
                Helper.ApplicationDeployment current = Helper.ApplicationDeployment.CurrentDeployment;
                this.Text = GlobalCode.GetApplicationName() + $" v{current.CurrentVersion}" + "   |   " +
                    oEmp.NickName + " " + oEmp.LastName + ", " + oEmp.JobCodeDescription;
            }
            else
            {
                this.Text = GlobalCode.GetApplicationName() + $" Dev_Version" + "   |   " +
                    oEmp.NickName + " " + oEmp.LastName + ", " + oEmp.JobCodeDescription;
            }

not sure whether ystem.Runtime.InteropServices.RuntimeInformation.FrameworkDescription; is you looking for.in net7, it return: ".NET 7.0.0" you can also use the System.Environment.Version

在此处输入图像描述

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