74

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

2

11 Answers 11

111

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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.
To get only the version number, append .ToString() to the end, e.g. [System.Reflection.Assembly]::LoadFrom("YourDllName.dll").GetName().Version.ToString()
Good answer. Q: How can I use that in the external tools of Visual Studio (Tools -> External Tools...) ?
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.
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.
|
21

If you use mono and linux, try this:

monodis --assembly MyAssembly.dll

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

1 Comment

If you are missing monodis, sudo apt-get install mono-utils.
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));
            }
        }
    }
}

3 Comments

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.
Is there any way to use this to get the output in a batch file variable?
@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.
9

In Powershell

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

2 Comments

The FileVersion is different than the AssemblyVersion
Yep, it's different, but you can get the actual version of package
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)

Comments

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

2 Comments

SigCheck is VERY nice, but it does not show AssemblyVersion.
where does filever.exe come from?
1

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.

Comments

0

File Version tool will help:

filever /V YourDllName.dll

2 Comments

What and where is this?
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'.
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

Comments

0

I used the accepted answer in Windows PowerShell, but using Powershell 7 (pwsh) the accepted answer failed as follows:

[System.Reflection.Assembly]::LoadFrom($(Get-Location).Path + "\MyApp\bin\Debug\MyApp.exe").GetName().Version.ToString()
MethodInvocationException: Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'C:\Users\user\work\repo\MyApp\bin\Debug\MyApp.exe'. Format of the executable (.exe) or library (.dll) is invalid."

I cobbled together an alternative after reading the comments on this Reddit thread.

[System.Reflection.AssemblyName]::GetAssemblyName($(Get-Location).Path + "\MyApp\bin\Debug\MyApp.exe").Version.ToString()
3.1.9120.17056

Specifically [System.Reflection.Assembly]::LoadFrom() is loading into the .NET environment that PowerShell is running in - ie unless they are compatible it will fail to load.

Consider that LoadFrom() implies loading in preparation to run code from it, then we just query GetName() and throw it away. The alternative form just queries the metadata to get the name without loading via [System.Reflection.AssemblyName]::GetAssemblyName().

Comments

-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>"

2 Comments

/l [ <assembly_name> ] Lists the contents of the global assembly cache. When the optional <assembly_name> parameter is specified only matching assemblies are listed.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.