change compiled assembly version information

青春壹個敷衍的年華 提交于 2019-12-13 01:54:04

问题


I have an exe and a bunch of dlls and they are not in strong naming.

The exe requires specific version of one of the dlls which has a different version so an error occurs when launching the exe.

I don't have the source code for this dll to change its assembly version so is it possible to change this dll's version or its reference within the exe externally?

I'd tried "ILMerge.exe Foo.dll /ver:1.2.3.4 /out:Foo2.dll" with the dll, but the resulting dll's version remains the same.

Any ideas?

Thanks,


回答1:


You can do this easily using Mono.Cecil (https://www.nuget.org/packages/Mono.Cecil/) Open the assembly using Mono.Cecil and look for the AssemblyFileVersionAttribute and make the necessary changes and then save the assembly (dll) and use the modified file.

In order to update the exe, you'd do something very similar to update the dependent (assembly) dll version number.

See below (updated to include the code sample to change the version of the assembly (dll) as well as the metadata in the 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);
}


来源:https://stackoverflow.com/questions/33664533/change-compiled-assembly-version-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!