如何从外部文件读取属性值?
我在构建过程中自动生成了 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 文件外部指定 Major
和 Minor
属性,因此我不必每次都卸载项目想换版本。我需要从项目内的特殊文本文件加载它们,或者以某种方式在项目属性对话框中设置它们。有什么建议吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 ReadLinesFromFile 在单独的文件中创建版本:
Used
ReadLinesFromFile
to make version in separate file:可以使用 PropertyFunctions 的功能来完成此操作 直接调用某些.NET 函数。本质上,您可以调用 File。设置属性值时的 ReadAllText()。
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.
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.
使用属性页 ,因此您可以在项目属性表对话框中设置这些属性。
您将需要创建一个属性文件并编辑项目文件(仅一次)以将导入指令添加到属性文件中。 这是一个示例。
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.
您可以使用外部工具
You can use your external tools
如果您关心的是 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.