Assembly version from command line?

49,534

Solution 1

This is an area where PowerShell shines. If you don't already have it, install it. It's preinstalled with Windows 7.

Running this command line:

[System.Reflection.Assembly]::LoadFrom("C:\full\path\to\YourDllName.dll").GetName().Version

outputs this:

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      8      0

Note that LoadFrom returns an assembly object, so you can do pretty much anything you like. No need to write a program.

Solution 2

If you use mono and linux, try this:

monodis --assembly MyAssembly.dll

find . -name MyAssembly.dll -exec monodis --assembly {} ';' | grep Version 

Solution 3

For those, like I, who come looking for such a tool:

using System;
using System.IO;
using System.Reflection;

class Program
{
    public static void Main(string[] args)
    {
        foreach (string arg in args)
        {
            try
            {
                string path = Path.GetFullPath(arg);
                var assembly = Assembly.LoadFile(path);
                Console.Out.WriteLine(assembly.GetName().FullName);
            }
            catch (Exception exception)
            {
                Console.Out.WriteLine(string.Format("{0}: {1}", arg, exception.Message));
            }
        }
    }
}

Solution 4

In Powershell

$version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("filepath.exe").FileVersion.ToString()

Solution 5

I used the selected answer until I got the following error Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. for several assemblies

using

[System.Reflection.Assembly]::ReflectionOnlyLoadFrom("C:\full\path\to\YourDllName.dll").GetName().Version

should work in those cases (probably all cases)

Share:
49,534
Patrice Pezillier
Author by

Patrice Pezillier

Updated on May 23, 2020

Comments

  • Patrice Pezillier
    Patrice Pezillier almost 4 years

    Is there a Microsoft tool to get the assembly version of a DLL file from a command line?

    (I know that I can code my own tool.)

  • Piotr Owsiak
    Piotr Owsiak over 12 years
    Such short pieces of code are best (and easiest to maintain) when kept as scripts. I highly recommend the cs-script project to achive this.
  • OregonGhost
    OregonGhost almost 12 years
    I would expect it to work similarly. My tests failed, though, with an error message that says that the runtime version of the mixed-mode assembly is newer than the one loaded - don't know how to circumvent this.
  • JJS
    JJS almost 10 years
    /l [ <assembly_name> ] Lists the contents of the global assembly cache. When the optional <assembly_name> parameter is specified only matching assemblies are listed.
  • Brannon
    Brannon almost 10 years
    What and where is this?
  • laifukang
    laifukang over 9 years
    It is in Windows XP Service Pack 2 Support Tools. HOWEVER, this can only be installed on an XP system. SO, to get the file for Win7/8, 'unzip' the installation exe with a tool like 7-Zip. Extract the 'support.cab' file inside. Then yet again, unpack this CAB using 7-Zip and you can get 'filever.exe'.
  • Rich Zwaap
    Rich Zwaap over 9 years
    To get only the version number, append .ToString() to the end, e.g. [System.Reflection.Assembly]::LoadFrom("YourDllName.dll").Ge‌​tName().Version.ToSt‌​ring()
  • Matt
    Matt almost 8 years
    Good answer. Q: How can I use that in the external tools of Visual Studio (Tools -> External Tools...) ?
  • Matt
    Matt almost 8 years
    That does not work for me if I have a local assembly, e.g. inside a NUGET package. Gacutil only works for assemblies in the GAC.
  • Richard P
    Richard P almost 7 years
    A couple of caveats to this solution. 1) It takes the full path to the DLL. The relative path in the example won't work. 2) It marks the assembly as "in use", which prevents it from being replaced until you exit that powershell process. That can block a build or deleting the folder, etc. To get around that either do it in a script or invoke "powershell" one more time first, then exit that child-process powershell when done.
  • GregHNZ
    GregHNZ almost 7 years
    For me on Windows 10, it would not recognise double quotes, and reported Missing ')' in method call then complained about the filename. It works fine, however, using single quotes. Also, on Windows 10 with single-quotes it will accept a relative path.
  • OzBob
    OzBob over 5 years
    add GetName() and see extended properties. See fuller example in my answer
  • Carl Walsh
    Carl Walsh over 5 years
    The FileVersion is different than the AssemblyVersion
  • whatsisname
    whatsisname about 5 years
    This answer will frequently no longer work because of .net runtime version mismatch shenanigans.
  • NickG
    NickG about 4 years
    Is there any way to use this to get the output in a batch file variable?
  • Paul Ruane
    Paul Ruane almost 3 years
    @NickG, you can (mis)use the for statement in Window batch to read output into variables. For example for /f %%a in ('time /t') do ( set currentTime=%%a ) would read the time into %%a within the loop then set it into %currentTime% where you can subsequently use it.
  • cskwg
    cskwg over 2 years
    SigCheck is VERY nice, but it does not show AssemblyVersion.