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.
CodePudding user response:
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();
}
}
CodePudding user response:
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