How to read property value from external file?

后端 未结 5 1713
死守一世寂寞
死守一世寂寞 2021-01-04 09:54

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


    2
    &l         


        
相关标签:
5条回答
  • 2021-01-04 10:33

    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.

    0 讨论(0)
  • 2021-01-04 10:38

    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.

    0 讨论(0)
  • 2021-01-04 10:39

    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)"/>
    
    0 讨论(0)
  • 2021-01-04 10:52

    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.

    0 讨论(0)
  • 2021-01-04 10:56

    You can use your external tools

    <Exec Command="newversion incMinor AssemblyInfo.cs > newversion.log" /> 
    
    0 讨论(0)
提交回复
热议问题