14

I have AssemblyInfo.cs file automatically generated during build. Here's part of .csproj file:

<PropertyGroup>
    <Major>2</Major>
    <Minor>3</Minor>
    <Build>0</Build>
    <Revision>0</Revision>
</PropertyGroup>
<Target Name="BeforeBuild">
    <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files\VisualSVN Server\bin">
        <Output TaskParameter="Revision" PropertyName="Revision" />
    </SvnVersion>
    <AssemblyInfo CodeLanguage="CS" 
                  OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs" 
                  AssemblyVersion="$(Major).$(Minor).$(Build).$(Revision)" 
                  AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)"/>
</Target>

But I don't know how to specify Major and Minor properties outside .csproj file so I don't have to unload project every time I want to change version. I need either to load them from special text file inside project or to somehow set them in project properties dialog. Any suggestions?

Poma
  • 8,174
  • 18
  • 82
  • 144
  • 1
    I want to be able to change my assembly version ('Major' and 'Minor' properties) without unloading project and editing .csproj file in notepad. – Poma Apr 21 '11 at 16:35

5 Answers5

16

Used ReadLinesFromFile to make version in separate file:

<ReadLinesFromFile File="Properties\Version.txt">
    <Output TaskParameter="Lines" ItemName="Ver" />
</ReadLinesFromFile>
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files (x86)\VisualSVN Server\bin">
    <Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<Message Text="Version: @(Ver).$(Revision)" />
<AssemblyInfo 
    CodeLanguage="CS" 
    OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs" 
    AssemblyVersion="@(Ver).$(Revision)" 
    AssemblyFileVersion="@(Ver).$(Revision)"/>
Poma
  • 8,174
  • 18
  • 82
  • 144
10

It is possible to do this using the ability of PropertyFunctions to call certain .NET functions directly. Essentially, you can call File.ReadAllText() when setting a property value.

<PropertyGroup>
    <Version>$([System.IO.File]::ReadAllText("Version.txt"))</Version>
</PropertyGroup>
<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files (x86)\VisualSVN Server\bin">
    <Output TaskParameter="Revision" PropertyName="Revision" />
</SvnVersion>
<Message Text="Version: $(Version).$(Revision)" />
<AssemblyInfo 
    CodeLanguage="CS" 
    OutputFile="$(MSBuildProjectDirectory)\Properties\VersionInfo.cs" 
    AssemblyVersion="$(Version).$(Revision)" 
    AssemblyFileVersion="$(Version).$(Revision)"/>

The Version.txt file would just contain the first three version numbers, i.e. "1.2.3" or whatever.

I'm not sure exactly when this was added to MSBuild, but it was probably after this question was asked and answered.

Nick Betts
  • 143
  • 1
  • 6
0

Use property pages, so you could set these properties on the project property sheets dialogs.

You will need to create a properties file and edit your project file (only once) to add an import directive to your properties file. Here is an example.

Amir Gonnen
  • 3,525
  • 4
  • 32
  • 61
  • 1
    This method applies only to C++ because C# uses Project Designer instead of property pages which is completely different. – Poma Jun 09 '11 at 15:04
0

You can use your external tools

<Exec Command="newversion incMinor AssemblyInfo.cs > newversion.log" /> 
Alexey
  • 31
  • 2
0

If it's locking of the csproj files by VS that's your concern, my answer to this question - How to turn off caching of build definitions in Visual studio might help you.

You could move the content of your BeforeBuild task (including the version propertygroup) into a separate proj file and invoke it with an MSBuild task (using a random filename generated by the example in the linked answer above). This would allow you to manually edit your version number properties without having to unload/load your csproj file.

Community
  • 1
  • 1
KazR
  • 961
  • 7
  • 16