68

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.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrice Pezillier
  • 4,476
  • 9
  • 40
  • 50
  • Does this answer your question? [Get file version and assembly version of DLL files in the current directory and all sub directories](https://stackoverflow.com/questions/3267009/get-file-version-and-assembly-version-of-dll-files-in-the-current-directory-and) – Michael Freidgeim May 31 '22 at 13:24
  • Is anyone else in need of an answer that would work on linux with only dotnet installed? – SandRock Jun 14 '23 at 14:29

10 Answers10

103

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.

Carlos A. Ibarra
  • 6,002
  • 1
  • 28
  • 38
OregonGhost
  • 23,359
  • 7
  • 71
  • 108
  • 2
    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. – OregonGhost Aug 15 '12 at 09:57
  • 2
    To get only the version number, append `.ToString()` to the end, e.g. `[System.Reflection.Assembly]::LoadFrom("YourDllName.dll").GetName().Version.ToString()` – Rich Zwaap Dec 03 '14 at 14:26
  • Good answer. Q: How can I use that in the external tools of Visual Studio (Tools -> External Tools...) ? – Matt Jul 28 '16 at 12:58
  • 6
    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. – Richard P Jul 17 '17 at 19:29
  • 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. – GregHNZ Aug 11 '17 at 02:07
  • add GetName() and see extended properties. See fuller example in my answer – OzBob Oct 12 '18 at 04:59
  • This answer will frequently no longer work because of .net runtime version mismatch shenanigans. – whatsisname Mar 07 '19 at 22:39
18

If you use mono and linux, try this:

monodis --assembly MyAssembly.dll

find . -name MyAssembly.dll -exec monodis --assembly {} ';' | grep Version 
head_thrash
  • 1,623
  • 1
  • 21
  • 26
11

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));
            }
        }
    }
}
John Smith
  • 496
  • 1
  • 6
  • 20
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
  • 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. – Piotr Owsiak Oct 14 '11 at 18:27
  • Is there any way to use this to get the output in a batch file variable? – NickG Apr 09 '20 at 14:46
  • @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. – Paul Ruane Jun 11 '21 at 15:24
8

In Powershell

$version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("filepath.exe").FileVersion.ToString()
Aussie Ash
  • 1,276
  • 12
  • 10
4

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)

gilmishal
  • 1,884
  • 1
  • 22
  • 37
1

Wow this is bad considering things like old exploitable gdiplus.dll's floating around.

My solution is simple. batch file programming.

This puts an nfo file in the same dir with the version

You can GET filever.exe, which can be downloaded as part of the Windows XP SP2 Support Tools package - only 4.7MB of download.

adobe_air_version.bat

c:\z\filever.exe /A /D /B "C:\Program Files\Common Files\Adobe AIR\Versions\1.0\Adobe AIR.dll" >000_adobe_air.dll_VERSION.nfo

exit

Variation.

Get all the versions in a directory to a text file.

c:\z\filever.exe /A /D /B "c:\somedirectory\ *.dll *.exe >000_file_versions.nfo

exit

There's also Sigcheck by systernals.

http://technet.microsoft.com/en-us/sysinternals/bb897441.aspx

andrewsi
  • 10,807
  • 132
  • 35
  • 51
0

File Version tool will help:

filever /V YourDllName.dll
Antony Thomas
  • 3,576
  • 2
  • 34
  • 40
  • What and where is this? – Brannon Jun 11 '14 at 18:46
  • It is in [Windows XP Service Pack 2 Support Tools](http://www.microsoft.com/en-us/download/details.aspx?id=18546). 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'. – laifukang Sep 22 '14 at 03:05
0

Adding some sugar to the other powershell-ish answers...

To get extended properties like 'FullName'

$dllPath = "C:\full\path\to\YourDllName.dll";
$ass  = [System.Reflection.Assembly]::LoadFrom($dllPath);
$ass.GetName();
$ass
OzBob
  • 4,227
  • 1
  • 39
  • 48
0

If the intention is to see the version, we can use the following command

ildasm "<your assembly path>"  /TEX | find "AssemblyFileVersionAttribute"

This return a string like the following .custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 0E 32 32 2E 38 2E 38 32 37 30 2E 31 36 35 // ...22.8.8270.165

The version is given in the comment.

Pramod B R
  • 612
  • 6
  • 7
-3

Do you use GACUTIL?

You can get the assembly version from this command below.

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\gacutil.exe /L "<your assembly name>"
Mzf
  • 5,210
  • 2
  • 24
  • 37
Radityo Ardi
  • 138
  • 6
  • /l [ ] Lists the contents of the global assembly cache. When the optional parameter is specified only matching assemblies are listed. – JJS Jun 11 '14 at 18:22
  • 1
    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. – Matt Jul 28 '16 at 13:17