MSBuild:如何从 AssmeblyInfo.cs 读取程序集版本和文件版本?
MSBuild: How to read Assembly Version and FIle Version from AssmeblyInfo.cs?
如何使用 MSBuild 脚本从 AssemblyInfo.cs 获取 AssmeblyVersion 和 FileVersion?
Using MSBuild script how can we get AssmeblyVersion and FileVersion from AssemblyInfo.cs?
要通过 MSBuild 获取 AssmeblyVersion
,您可以使用 GetAssemblyIdentity 任务。
为此,请卸载您的项目。然后在项目的最后,就在 end-tag </Project>
之前,将脚本放在下面:
<Target Name="GetAssmeblyVersion" AfterTargets="Build">
<GetAssemblyIdentity
AssemblyFiles="$(TargetPath)">
<Output
TaskParameter="Assemblies"
ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<Message Text="Assmebly Version: %(MyAssemblyIdentities.Version)"/>
</Target>
要获得 FileVersion
,您可以添加自定义 MSBuild Inline Tasks 来获得它,编辑您的项目文件 .csproj
,添加以下代码:
<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>
完成这两个目标后,您将从 AssemblyInfo.cs
:
获得 AssmeblyVersion
和 FileVersion
注意:如果要获取特定dll的版本,可以将AssemblyFiles="$(TargetPath)"
改为:
<PropertyGroup>
<MyAssemblies>somedll\the.dll</MyAssemblies>
</PropertyGroup>
AssemblyFiles="@(MyAssemblies)"
希望这对您有所帮助。
如何使用 MSBuild 脚本从 AssemblyInfo.cs 获取 AssmeblyVersion 和 FileVersion?
Using MSBuild script how can we get AssmeblyVersion and FileVersion from AssemblyInfo.cs?
要通过 MSBuild 获取 AssmeblyVersion
,您可以使用 GetAssemblyIdentity 任务。
为此,请卸载您的项目。然后在项目的最后,就在 end-tag </Project>
之前,将脚本放在下面:
<Target Name="GetAssmeblyVersion" AfterTargets="Build">
<GetAssemblyIdentity
AssemblyFiles="$(TargetPath)">
<Output
TaskParameter="Assemblies"
ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<Message Text="Assmebly Version: %(MyAssemblyIdentities.Version)"/>
</Target>
要获得 FileVersion
,您可以添加自定义 MSBuild Inline Tasks 来获得它,编辑您的项目文件 .csproj
,添加以下代码:
<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>
完成这两个目标后,您将从 AssemblyInfo.cs
:
AssmeblyVersion
和 FileVersion
注意:如果要获取特定dll的版本,可以将AssemblyFiles="$(TargetPath)"
改为:
<PropertyGroup>
<MyAssemblies>somedll\the.dll</MyAssemblies>
</PropertyGroup>
AssemblyFiles="@(MyAssemblies)"
希望这对您有所帮助。