1

I am trying to set the version number to something I want (read they want me to do). They want a specific format, and I need to set the version of out programme.

I tried to use a UsingTask to set a PropertyGroup variable. My UsingTask is working.

All this is new to me, I managed to get the code below. Now, how can I set the local project variable? My initial value is used, next I want to set it programmatically in the UsingTask

This is all done in the *.csproj file

<Project Sdk="Microsoft.NET.Sdk">

<!-- my additional project variable, used below -->
  <PropertyGroup Label="UserMacros">
    <AssemblyVersion>1.2.3.5</AssemblyVersion>
  </PropertyGroup>

  <UsingTask TaskName="EnvVarSet"
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[
// here I want to set the version number to our format
          Log.LogMessage(MessageImportance.High, "**** Setting variables");
          System.Environment.SetEnvironmentVariable("AssemblyVersion", "1.5.6.7");                 
          var a = Environment.GetEnvironmentVariable("AssemblyVersion");
          Log.LogMessage(MessageImportance.High, "**** Value is: " +a);
        ]]>
      </Code>
    </Task>    
  </UsingTask>
  
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
<!-- the version number I want to set -->
    <Version>$(AssemblyVersion)</Version>
<!-- this does not work -->
<!--    <Version>$([System.Environment]::GetEnvironmentVariable('AssemblyVersion'))</Version>-->
other values....
  </PropertyGroup>

//more project....

  <Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
    <EnvVarSet />
    <Message Text="***** AssemblyVersionis now: $([System.Environment]::GetEnvironmentVariable('AssemblyVersion'))"/>
  </Target>
</Project>
4
  • So during the build of your csproj you want a script inside your .csproj file to change said .csproj file? That sounds like a bad idea to me. Commented Aug 25, 2022 at 15:48
  • @GoodNightNerdPride well, do you have any other ideas how to set the version number to something custom? Commented Aug 25, 2022 at 15:55
  • What do you mean by custom? A custom version or a custom place to store it? Commented Aug 27, 2022 at 6:34
  • 1
    @GoodNightNerdPride I guess they're referring to the ".Net Framework project style" where you would set Assembly Information via a button in the project properties. Where you would set the version in the project file and not at compile time. Commented Nov 17, 2022 at 13:38

3 Answers 3

1

You can view the document https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build. You can do this by using the dotnct command. Use multiple doctnct commands to pass the version number to each command and build the project and all its dependencies via dotnet build-. You can use the following command to help you set the version number: dotnet build /p:AssemblyVersion=1.2.3.4

Sign up to request clarification or add additional context in comments.

Comments

0

I managed to get what I want. Here is my example of how to do the classic Build and Revision autoincrement numbers in the assembly version This is an example, I was asked to do another format, but one can create any format this way

First, add the AssemblyVersion

<PropertyGroup>
  <!-- add this line -->
  <AssemblyVersion>1.0.0.0</AssemblyVersion>

And add this code, which is similar to "1.2.*" in .Net Framework

    <Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
        <SetBuildNumber>
          <Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion" />
        </SetBuildNumber>
      </Target>
      <UsingTask TaskName="SetBuildNumber" TaskFactory="CodeTaskFactory"
        AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
          <AssemblyVersion ParameterType="System.String" Output="true" />
        </ParameterGroup>
        <Task>
          <Code Type="Fragment" Language="cs">
            <![CDATA[
              Log.LogMessage(MessageImportance.High, "Setting version number");
             
              var now = DateTime.Now;
              var secondsSinceMidnightDivivedBy2 = (int)(now - now.Date).TotalSeconds / 2;
              var daysSinceJan1st2000 = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
              AssemblyVersion = "1.2." + 
                daysSinceJan1st2000.ToString() + "." + 
                secondsSinceMidnightDivivedBy2.ToString();
    
              Log.LogMessage(MessageImportance.High, "Version number is: " + AssemblyVersion);
            ]]>
          </Code>
        </Task>
      </UsingTask>

In the code above, one can set the assembly version to what may be required

Comments

0

The code above give a custom assembly version to the final exe or dll file, but for some reason it does not work, when the installer publishes the project to one file, in the "pre-build event command line":

    dotnet publish $(SolutionDir)NCAutomatedReport\NCAutomatedReport.csproj
 -p:PublishProfile=FolderProfile -r:win10-x64 -p:PublishSingleFile=true
 -p:PublishReadyToRun=false -p:PublishTrimmed=false -c:Release 
 -o:$(TargetDir)published

This gives the error

The task factory "CodeTaskFactory" is not supported on the .NET Core version of MSBuild.

and

The task factory "CodeTaskFactory" could not be loaded from the assembly
"C:\Program Files\dotnet\sdk\6.0.400\Microsoft.Build.Tasks.Core.dll".
The task factory must return a value for the "TaskType" property.   

Now I should look into this - any ideas anyone?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.