2011-04-21 44 views
10

我在構建期間自動生成了AssemblyInfo.cs文件。下面是.csproj的文件的一部分:如何從外部文件讀取屬性值?

<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> 

但我不知道如何指定的.csproj文件外MajorMinor性質,所以我沒有我想改變版本每次卸載項目。我需要從項目中的特殊文本文件加載它們,或者以某種方式在項目屬性對話框中設置它們。有什麼建議麼?

+0

我希望能夠改變我的程序集的版本(「重大」和「次要」的屬性),而在記事本文件.csproj的項目卸載和編輯。 – Poma 2011-04-21 16:35:58

回答

13

用於ReadLinesFromFile在單獨的文件,以使版本:

<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

使用property pages,因此您可以在項目屬性表對話框中設置這些屬性。

您將需要創建一個屬性文件並編輯您的項目文件(僅一次)以將導入指令添加到您的屬性文件中。 Here is an example

+1

此方法僅適用於C++,因爲C#使用Project Designer而不是完全不同的屬性頁面。 – Poma 2011-06-09 15:04:31

0

您可以使用外部工具

<Exec Command="newversion incMinor AssemblyInfo.cs > newversion.log" /> 
0

如果它被鎖定在VS文件的csproj這是你的關心,我的回答這個問題的 - How to turn off caching of build definitions in Visual studio可能會幫助您。

您可以將您的BeforeBuild任務(包括版本屬性組)的內容移動到單獨的proj文件中,並使用MSBuild任務(使用上面鏈接的答案中的示例生成的隨機文件名)調用它。這將允許您手動編輯版本號屬性,而無需卸載/加載您的csproj文件。

相關問題