During a setup creation process, I am planning to do the following (within a C# Script):
- Read the
AssemblyVersion
andAssemblyFileVersion
attribute values of one DLL. - Iterate all DLLs and executables in the project and apply those version to their
AssemblyVersion
andAssemblyFileVersion
attribute values.
Question now is: How to do step 2?
I successfully can do step 1, but for step 2 I don't see a real start point. What I probably have to do is to use some native P/Invoke methods since the attributes directly modify the version information resource information of a DLL/executable.
Any hints on this?
Why don't you, during build process, read the AssemblyVersion
and AssemblyFileVersion
of one DLL, and save it back to other csproject's AssemblyInfo.cs
, only then compile it?
As a matter of fact, I don't know whether it is possible to modify the DLL file directly, without resorting to something fanciful.
Or alternatively, make sure that all your DLLs share one common AssemblyInfo.cs
. You can do this by adding the AssemblyInfo.cs
as "Add As Link" when you add a new item in csproject. In this way when you do a compilation, all the DLLs will share the same AssemblyInfo.cs
, and thus output the same AssemblyVersion
.
In my case, I created a T4 template for changing the AssemblyVersion and AssemblyFileVersion. I called the template Assembly.tt, and made it a linked file (when you add it choose Add Link, instead of Add). All my assemblies pull in the linked Assembly.tt file.
You can then run the T4 template from one location and it will update all the AssemblyVersion and AssemblyFileVersions. You don't have to call the file AssemblyInfo.cs for the information to be pulled into your dlls.
The code for the Assembly.tt file is:
<#@ template language="C#" hostspecific="true" #>
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//
using System.Reflection;
[assembly: AssemblyVersion("4.<#= this.RevisionYear #>.<#= this.RevisionNumber #>.<#= this.RevisionTime #>")]
[assembly: AssemblyFileVersion("4.<#= this.RevisionYear #>.<#= this.RevisionNumber #>.<#= this.RevisionTime #>")]
<#+
int RevisionYear = DateTime.UtcNow.Year;
int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(DateTime.UtcNow.Year,1,1)).TotalDays;
int RevisionTime = (int)(DateTime.UtcNow - new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day)).TotalMinutes;
#>
The output of the T4 template above will be:
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//
using System.Reflection;
[assembly: AssemblyVersion("4.2016.284.1066")]
[assembly: AssemblyFileVersion("4.2016.284.1066")]
If you have access to the sources, take the advice from Graviton
If you don't, you might be in trouble. Possibly you can disassemble with ILDASM and reassemble with ILASM. But this won't work out for strong-named assemblies.
来源:https://stackoverflow.com/questions/1550249/programmatically-change-the-assemblyversion-and-assemblyfileversion-attributes