1

I created a simple Console-Application project in .Net 4.5 and have been assigning version via command like this:

msbuild ConsoleApplication1.csproj /property:ApplicationVersion=1.0.0.12

The build works fine. But when I print the version like this:

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Console.Write(version);

0.0.0.0 is printed instead of the actual version.

The following is the project-file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{AD9395EC-0419-482B-8A2E-F7B085AE444F}</ProjectGuid>
        <OutputType>Exe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ConsoleApplication1</RootNamespace>
        <AssemblyName>ConsoleApplication1</AssemblyName>
        <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
    </PropertyGroup>
    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />
    </ItemGroup>
    <ItemGroup>
        <Compile Include="Program.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
    </ItemGroup>
    <ItemGroup>
      <None Include="packages.config" />
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
      <PropertyGroup>
        <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
      </PropertyGroup>
      <Error Condition="!Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets'))" />
    </Target>
    <Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets')" />
    <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
         Other similar extension points exist, see Microsoft.Common.targets.
    <Target Name="BeforeBuild">
    </Target>
    <Target Name="AfterBuild">
    </Target>
    -->
    <PropertyGroup>
        <MSBuildCommunityTasksPath>$(SolutionDir)\packages/MSBuildTasks.1.5.0.235\tools</MSBuildCommunityTasksPath>
    </PropertyGroup>

    <PropertyGroup>
        <VersionNumber>1.0.0.1</VersionNumber>
    </PropertyGroup>

    <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets" />
    <!-- update standard assembly attribute in all projects -->
    <Target Name="BeforeBuild" >
        <Message Text="Updating AssemblyInfo to Version $(VersionNumber)"></Message>
        <Message Text="Writing to AssemblyInfo files in $(SolutionRoot)"></Message>
        <AssemblyInfo CodeLanguage="C#"
                      AssemblyVersion="$(VersionNumber)"
                      AssemblyFileVersion="$(VersionNumber)"
        >
        </AssemblyInfo>
    </Target>
</Project>
Orace
  • 7,822
  • 30
  • 45
Nishanth Reddy
  • 589
  • 2
  • 14
  • 27
  • Just out of curiosity, why are you doing it with the MSBuild command line and not an assemblyinfo file where it would happen automatically? – rory.ap Jan 21 '20 at 13:06
  • Have you tried `1.0.0.1` in place of ` ...` ? – Manuel Fabbri Jan 21 '20 at 13:16
  • @ManuelFabbri, he use `VersionNumber` lower in an `AssemblyInfo` block. This doesn't have any effect since the block have to be *Output* in a file that will be compiled like it's done here : https://stackoverflow.com/a/3603583/361177 – Orace Jan 21 '20 at 13:38
  • `ApplicationVersion` isn't defined in `csproj` file, try to use `/p:VersionNumber=1.0.0.12` – Pavel Anikhouski Jan 21 '20 at 13:49

1 Answers1

0

According to this documentation (search for ApplicationVersion in the page), it's look like that the ApplicationVersion passed to msbuild via parameter or in the *.csproj file specifies the version of the ClickOnce application.

Retrieve the ClickOnce application version at run time doesn't seem to be doable

edit:

To set the AssemblyVersion from a msbuild command line argument, you have to add a task that get the parameter value and put it in a AssemblyInfo.cs-lookalike file, like it's done here.

Orace
  • 7,822
  • 30
  • 45