更改编译的程序集版本信息
change compiled assembly version information
我有一个 exe 和一堆 dll,它们没有强命名。
exe 需要具有不同版本的 dll 之一的特定版本,因此在启动 exe 时会发生错误。
我没有此 dll 的源代码来更改其程序集版本,因此是否可以从外部更改此 dll 的版本或其在 exe 中的引用?
我尝试 "ILMerge.exe Foo.dll /ver:1.2.3.4 /out:Foo2.dll" 使用 dll,但生成的 dll 版本保持不变。
有什么想法吗?
谢谢,
您可以使用 Mono.Cecil (https://www.nuget.org/packages/Mono.Cecil/) 轻松完成此操作
使用 Mono.Cecil 打开程序集并查找 AssemblyFileVersionAttribute 并进行必要的更改,然后保存程序集 (dll) 并使用修改后的文件。
为了更新 exe,您需要执行非常类似于更新相关(程序集)dll 版本号的操作。
见下文(已更新以包括代码示例以更改程序集 (dll) 的版本以及 exe 中的元数据):
void Main(){
UpdateDll();
UpdateExe();
}
static void UpdateExe(){
var exe = @"C:\temp\sample.exe";
AssemblyDefinition ass = AssemblyDefinition.ReadAssembly(exe);
var module = ass.Modules.First();
var modAssVersion = module.AssemblyReferences.First(ar => ar.Name == "ClassLibrary1");
module.AssemblyReferences.Remove(modAssVersion);
modAssVersion.Version = new Version(4,0,3,0);
module.AssemblyReferences.Add(modAssVersion);
ass.Write(exe);
}
static void UpdateDll()
{
String assemblyFile = @"C:\temp\assemblyName.dll";
AssemblyDefinition modifiedAss = AssemblyDefinition.ReadAssembly(assemblyFile);
var fileVersionAttrib = modifiedAss.CustomAttributes.First(ca => ca.AttributeType.Name == "AssemblyFileVersionAttribute");
var constArg = fileVersionAttrib.ConstructorArguments.First();
constArg.Value.Dump();
fileVersionAttrib.ConstructorArguments.RemoveAt(0);
fileVersionAttrib.ConstructorArguments.Add(new CustomAttributeArgument(modifiedAss.MainModule.Import(typeof(String)), "4.0.3.0"));
modifiedAss.Name.Version = new Version(4,0,3,0);
modifiedAss.Write(assemblyFile);
}
我有一个 exe 和一堆 dll,它们没有强命名。
exe 需要具有不同版本的 dll 之一的特定版本,因此在启动 exe 时会发生错误。
我没有此 dll 的源代码来更改其程序集版本,因此是否可以从外部更改此 dll 的版本或其在 exe 中的引用?
我尝试 "ILMerge.exe Foo.dll /ver:1.2.3.4 /out:Foo2.dll" 使用 dll,但生成的 dll 版本保持不变。
有什么想法吗?
谢谢,
您可以使用 Mono.Cecil (https://www.nuget.org/packages/Mono.Cecil/) 轻松完成此操作 使用 Mono.Cecil 打开程序集并查找 AssemblyFileVersionAttribute 并进行必要的更改,然后保存程序集 (dll) 并使用修改后的文件。
为了更新 exe,您需要执行非常类似于更新相关(程序集)dll 版本号的操作。
见下文(已更新以包括代码示例以更改程序集 (dll) 的版本以及 exe 中的元数据):
void Main(){
UpdateDll();
UpdateExe();
}
static void UpdateExe(){
var exe = @"C:\temp\sample.exe";
AssemblyDefinition ass = AssemblyDefinition.ReadAssembly(exe);
var module = ass.Modules.First();
var modAssVersion = module.AssemblyReferences.First(ar => ar.Name == "ClassLibrary1");
module.AssemblyReferences.Remove(modAssVersion);
modAssVersion.Version = new Version(4,0,3,0);
module.AssemblyReferences.Add(modAssVersion);
ass.Write(exe);
}
static void UpdateDll()
{
String assemblyFile = @"C:\temp\assemblyName.dll";
AssemblyDefinition modifiedAss = AssemblyDefinition.ReadAssembly(assemblyFile);
var fileVersionAttrib = modifiedAss.CustomAttributes.First(ca => ca.AttributeType.Name == "AssemblyFileVersionAttribute");
var constArg = fileVersionAttrib.ConstructorArguments.First();
constArg.Value.Dump();
fileVersionAttrib.ConstructorArguments.RemoveAt(0);
fileVersionAttrib.ConstructorArguments.Add(new CustomAttributeArgument(modifiedAss.MainModule.Import(typeof(String)), "4.0.3.0"));
modifiedAss.Name.Version = new Version(4,0,3,0);
modifiedAss.Write(assemblyFile);
}