使用MSBuild脚本我们如何从AssemblyInfo.cs获取AssmeblyVersion和FileVersion?
答案 0 :(得分:0)
使用MSBuild脚本我们如何从AssemblyInfo.cs获取AssmeblyVersion和FileVersion?
要通过MSBuild获取</Project>
,您可以使用GetAssemblyIdentity任务。
要完成此操作,请卸载您的项目。然后在项目的最后,就在结束标记 <Target Name="GetAssmeblyVersion" AfterTargets="Build">
<GetAssemblyIdentity
AssemblyFiles="$(TargetPath)">
<Output
TaskParameter="Assemblies"
ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<Message Text="Assmebly Version: %(MyAssemblyIdentities.Version)"/>
</Target>
之前,放在脚本下面:
FileVersion
要获取.csproj
,您可以添加自定义MSBuild Inline Tasks来获取此内容,编辑项目文件 <UsingTask
TaskName="GetFileVersion"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<Version ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Diagnostics" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Log.LogMessage("Getting version details of assembly at: " + this.AssemblyPath, MessageImportance.High);
this.Version = FileVersionInfo.GetVersionInfo(this.AssemblyPath).FileVersion;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="GetFileVersion" AfterTargets="Build">
<GetFileVersion AssemblyPath="$(TargetPath)">
<Output TaskParameter="Version" PropertyName="MyAssemblyFileVersion" />
</GetFileVersion>
<Message Text="File version is $(MyAssemblyFileVersion)" />
</Target>
,添加以下代码:
AssmeblyVersion
完成这两个目标后,您将从FileVersion
获得AssemblyInfo.cs
和AssemblyFiles="$(TargetPath)"
:
注意:如果您想获取特定dll的版本,可以将<PropertyGroup>
<MyAssemblies>somedll\the.dll</MyAssemblies>
</PropertyGroup>
AssemblyFiles="@(MyAssemblies)"
更改为:
{{1}}
希望这有帮助。