Change Assembly Version in a compiled .NET assembly

Viewed 44162

Simple question... is there a way to change the Assembly Version of a compiled .NET assembly?

I'd actually be fine with a way to change the Assembly File Version.

7 Answers

You can use ILMerge:

ILMerge.exe Foo.dll /ver:1.2.3.4 /out:Foo2.dll

A valid reason to do this is to increment the assembly version in a build in you find breaking changes (using NDepend for example). That way if there are no breaking changes the assembly version stays the same, and you can patch released builds easily.

We always increment the file version, and that reflects the build number.

Why do you want to do this? If it's so that another application can use it, you might want to look into assembly binding redirection instead.

It sounds like your process is heavy because you have to update multiple AssemblyInfo files. Have you considered sharing the same AssemblyInfo file between projects? Derik Whittaker gives a good example on how to do this.

Once you have a single file, you could then go the extra distance by having a build process update your single AssemblyInfo version using MSBuild or NAnt.

Its strange folks have missed Resource Hacker. It is specially made for hacking file resources. So it will also help you edit assembly information including File Version.

using System;
using System.Windows.Forms;
using System.Diagnostics;

public class rh
{
    public static void Main()
    {
        Assembly_Changer("file_path.exe");
    }
    
    private static string rc()
    {
        string FileVersion = "6,8,0,1";
        return "1 VERSIONINFO"
        + "\n FILEVERSION " + FileVersion
        + "\n PRODUCTVERSION 0,0,0,0" 
        + "\n FILEOS 0x4"
        + "\n FILETYPE 0x1"
        + "\n {BLOCK \"StringFileInfo\"{BLOCK \"000004b0\"{}}BLOCK \"VarFileInfo\"{VALUE \"Translation\", 0x0000 0x04B0}}";
    }

    public static void Assembly_Changer(string exe)
    {
        System.IO.File.WriteAllText("details.rc", rc());
        process("reshacker.exe", "-open details.rc -save resources.res -action compile -log NUL");
        process("reshacker.exe", "-open \"" + exe + "\" -resource resources.res -save \"" + exe + "\" -action addoverwrite -mask \"Version info\"");
    }

    public static bool process(string filename, string args)
    {
        using (Process process = new Process())
        {
            process.StartInfo.FileName = filename;
            process.StartInfo.Arguments = args;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.Start();
            process.WaitForExit();
        }
        return true;
    }   
}
Related