Determine Assembly Version During a Post-Build Event

Determine assembly version during a post-build event

If you prefer scripting these methods might also work for you:

If you are using the post-build event, you can use the filever.exe tool to grab it out of the already built assembly:

for /F "tokens=4" %%F in ('filever.exe /B /A /D bin\debug\myapp.exe') do (
set VERSION=%%F
)
echo The version is %VERSION%

Get filever.exe from here: http://support.microsoft.com/kb/913111

If you are using the pre-build event, you can take it out of the AssemblyInfo.cs file as follows:

set ASMINFO=Properties\AssemblyInfo.cs
FINDSTR /C:"[assembly: AssemblyVersion(" %ASMINFO% | sed.exe "s/\[assembly: AssemblyVersion(\"/SET CURRENT_VERSION=/g;s/\")\]//g;s/\.\*//g" >SetCurrVer.cmd
CALL SetCurrVer.cmd
DEL SetCurrVer.cmd
echo Current version is %CURRENT_VERSION%

This uses the unix command line tool sed, which you can download from many places, such as here: http://unxutils.sourceforge.net/ - iirc that one works ok.

How to get version number in post-build event

This post build script works for me. It packages and publishes my dev versions on each build. Pretty simple.

del $(ProjectDir)bin\Debug\*.nupkg
"$(ProjectDir)NuGet.exe" pack "$(ProjectDir)MyProject.csproj"
forfiles /P $(ProjectDir)bin\Debug\ /m *.nupkg /c "cmd /c "$(ProjectDir)NuGet.exe" push @FILE -Source \\SHARE\NuGet"

Read Assembly Version in a Post Build Event in Visual Studio 2022 targeting Netstandard2 .0

After try and error i found that the attribute DependsOnTargets="PostBuildMacros" was missing, the following post build event solved my problem:

<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="@(Targets->'%(Version)')" />
</ItemGroup>
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" DependsOnTargets="PostBuildMacros">
<Exec Command="$(ExecNuget) pack $(ProjectPath) $(ExecNuget) push -Source "YourSourceName" -ApiKey "YourKey" $(ProjectDir)$(ProjectName).@(VersionNumber).nupkg" />
</Target>

to avoid compiling errors replace "YourSourceName" and "YourKey" with your repository data

Is it possible to get Product Version in post build event?

You're looking for Read AssemblyFileVersion from AssemblyInfo post-compile. You're going to need a custom task for this, since GetAssemblyIdentity doesn't return the productversion.

The linked question has the answer for AssemblyFileVersion, it shouldn't be too hard to adapt it to make it return the ProductVersion.

Accessing AssemblyVersion number from assembly information to use in the post build event command line

They are declared in the AssemblyInfo.cs file and embedded in the $(TargetPath). You could write a little utility to dig it out again. For example:

using System;
using System.Reflection;

class Program {
static void Main(string[] args) {
if (args.Length == 0) throw new Exception("Assembly path required");
var asm = Assembly.LoadFile(args[0]);
Console.WriteLine(asm.GetName().Version.ToString());
var vers = (AssemblyFileVersionAttribute)asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)[0];
Console.WriteLine(vers.Version);
}
}

Post build event ought to look something like this:

c:\bin\myutil.exe $(TargetPath)

How can I get the Product Name in a Visual Studio post build event?

For C/C++ projects,

Use the Property Manager (in the menu bar: View/Other Windows/Property Manager) to manage your projects' properties.

You can define a custom marco like $(ProjectName) in a new property sheet. And share the property sheet among your projects. Then you can use the marco in the post build event of every project.

For C# projects,

Use the Property Functions.

In Visual Studio 2012 (C#) How to Create Custom Macros available in Post Build

MSBuild: Permanently Change PropertyGroup Property of a Project

By the way, you can use the environment variables if it is good for you.

Getting version in post-build for Nuget

It is not clear in your question, but assuming you want to sync the version of your package with the version of your assembly, you can simply manage the AssemblyVersion attribute in the project's AssemblyInfo.cs file.

[assembly: AssemblyVersion("1.0.0")]

or if you want to use auto-generated build numbers

[assembly: AssemblyVersion("1.0.0.*")]

If you want to deviate from the assembly's version, and only specify the package version, you can use the AssemblyInformationalVersion attribute in the AssemblyInfo.cs file.

[assembly: AssemblyInformationalVersion("1.0.0")]

It's also not clear from the question what versioning strategy you use, but I'll assume you want to apply Semantic Versioning (where the first 3 version numbers are most relevant). In general when auto-creating NuGet packages, I'd recommend you to create a tokenized nuspec file in your csproj directory, so you can more easily manipulate the package metadata. FYI, there's even a NuGet package to assist you with that:

Install-Package NuSpec 

NuGet will look for this nuspec (make sure it's called MyProject.nuspec) when targeting MyProject.csproj.

<package>
<version>$version$</version>
...
</package>

I also explained this on the MyGet blog in this post: http://blog.myget.org/post/2012/04/27/NuGet-version-token-explained.aspx

A post build that calls nuget pack should be good enough then, assuming you simply change the assembly version before building.

nuget pack MyProject.csproj


Related Topics



Leave a reply



Submit