How to read property value from external file?

Poma picture Poma · Apr 21, 2011 · Viewed 9.8k times · Source

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?

Answer

Poma picture Poma · Jun 9, 2011

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)"/>