简体   繁体   中英

Reference AssemblyFileVersion in csproj file

Once my nant build has completed I'd like to rename the generated .exe file using a post-build command which is appended to the end of the projects .csproj file (within the Project element):

<Target Name="AfterBuild">
  <Copy SourceFiles="$(TargetDir)\$(TargetName).exe" DestinationFiles="$(TargetDir)\MyApplication-$(AssemblyFileVersion).exe" SkipUnchangedFiles="true" />
</Target>

As you can see above, I am trying to rename the executable to: MyApplication-$(AssemblyFileVersion).exe however, this is obviously wrong, as the resulting executable is simply named: MyApplication-.exe (so the version I am trying to add at the end of the file name is missing).

I have defined the AssemblyFileInfoVersion in the AssemblyInfo.cs file as follows:

[assembly: AssemblyFileVersion("1.5.1")]

So the question is: How can I access the AssemblyFileVersion in the csproj file of that same project?

GetAssemblyIdentity can get information about complied assemblies. The task output contain metadata entries about Version, PublicKeyToken, and Culture.

I used $(TargetDir)\\$(TargetName).exe as the assembly file.

<ItemGroup>
    <AssembliesPath Include="$(TargetDir)\$(TargetName).exe" />
</ItemGroup>

<Target Name="GetAssemblyInfo">
    <GetAssemblyIdentity AssemblyFiles="@(AssembliesPath)">
        <Output TaskParameter="Assemblies" ItemName="AssemblyInfo"/>
    </GetAssemblyIdentity>
</Target>

And then:

<Target Name="AfterBuild">
    <GetAssemblyInfo />
    <Copy SourceFiles="$(TargetDir)\$(TargetName).exe" DestinationFiles="$(TargetDir)\MyApplication-%(AssemblyInfo.Version).exe" SkipUnchangedFiles="true" />
</Target>

The following code is from ISun's original answer and this is actually how I ended up doing it in the end, as I had problems defining a custom task (ms build references were constantly auto-kicked and the build kept failing over and again).

As you can see from the comments under ISun's answer I always got the version 0.0.0.0 - despite having changed the version for AssemblyFileVersion and AssemblyVersion to 1.0.0.0 by manually opening the AssemblyInfo.cs in a texteditor. I later read how to edit the AssemblyInfo.cs from my Visual Studio, here is how:

  • Right-click on your project (that generates the exe file)
  • Click on Properties
  • Open the Application tab (first tab on the left) on the window that'll open
  • You'll see fields for setting the Assembly name, Default namespace etc however to edit the AssemblyInfo.cs, simply click on the Button called Assembly Information to the right

And for some reason - I have no clue why it suddenly worked, after I had put in all the information via the above method (using Visual Studio) ! When I first opened the AssemblyInfo.cs using the above way, all my fields were actually empty, despite them being filled in the actual file.

And now that I got the AssemblyInfo.cs to finally function correctly, I used ISun's original code to achieve my goal. By adding the following snippet just before the closing tag in my project's .csproj file:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

<Target Name="AfterBuild">
    <GetAssemblyIdentity AssemblyFiles="$(TargetDir)\$(TargetName).exe">
        <Output TaskParameter="Assemblies" ItemName="AssemblyInfo"/>
    </GetAssemblyIdentity>
    <Copy SourceFiles="$(TargetDir)\$(TargetName).exe" DestinationFiles="$(TargetDir)\MyApplication-%(AssemblyInfo.Version).exe" SkipUnchangedFiles="true" />
</Target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM