如何从外部文件读取属性值?

发布于 2024-11-02 14:10:31 字数 950 浏览 0 评论 0原文

我在构建过程中自动生成了 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 属性,因此我不必每次都卸载项目想换版本。我需要从项目内的特殊文本文件加载它们,或者以某种方式在项目属性对话框中设置它们。有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

つ低調成傷 2024-11-09 14:10:31

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

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)"/>
幽蝶幻影 2024-11-09 14:10:31

可以使用 PropertyFunctions 的功能来完成此操作 直接调用某些.NET 函数。本质上,您可以调用 File。设置属性值时的 ReadAllText()

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

Version.txt 文件仅包含前三个版本号,即“1.2.3”或其他。

我不确定这是什么时候添加到 MSBuild 中的,但可能是在提出并回答这个问题之后。

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.

烟酒忠诚 2024-11-09 14:10:31

使用属性页 ,因此您可以在项目属性表对话框中设置这些属性。

您将需要创建一个属性文件并编辑项目文件(仅一次)以将导入指令添加到属性文件中。 这是一个示例

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.

书信已泛黄 2024-11-09 14:10:31

您可以使用外部工具

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

You can use your external tools

<Exec Command="newversion incMinor AssemblyInfo.cs > newversion.log" /> 
温馨耳语 2024-11-09 14:10:31

如果您关心的是 VS 锁定的 csproj 文件,我对这个问题的回答 - 如何在 Visual Studio 中关闭构建定义的缓存可能会对您有所帮助。

您可以将 BeforeBuild 任务的内容(包括版本属性组)移动到单独的 proj 文件中,并使用 MSBuild 任务调用它(使用上面链接答案中的示例生成的随机文件名)。这将允许您手动编辑版本号属性,而无需卸载/加载 csproj 文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文