34

Is there any way to obtain the AssemblyVersion of a .Net executable in Linux without using mono? What I am trying to have is a script or command that will let me obtain the AssemblyVersion on Linux boxes. I tried:

#strings file.exe | grep AssemblyVersion
but it only the string and not the number. Also checked with:
#file file.exe
but only got general information.

Any ideas?

Freddy
  • 3,064
  • 3
  • 26
  • 27

6 Answers6

28

Try to match version numbers that span a whole line:

$ strings file.exe | egrep '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'

In my (few) tests, the AssemblyVersion of the binary was always the last result.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • I tested it but none of the numbers I am getting are the AssemblyVersion. In fact, I don't see the AssemblyVerson anywhere in the strings output. – Freddy Oct 18 '10 at 16:53
  • 3
    Use `-el` on the strings command to interpret the strings as unicode. – Nick Oct 22 '14 at 07:46
14

As recommended by Jb Evain you can use the Mono Disassembler.

monodis --assembly file.exe | grep Version
Jacob Foshee
  • 2,704
  • 2
  • 29
  • 48
  • For compatibility with a wider sample of assemblies, use `ikdasm -assembly` (also distributed with Mono) instead of `monodis --assembly`. – Otto G Oct 19 '18 at 08:23
6

Another option is to use exiftool, as in:

$ exiftool -AssemblyVersion -ProductVersion ./System.Private.CoreLib.dll
Assembly Version                : 7.0.0.0
Product Version                 : 7.0.4+0a396acafe9a7d46bce11f4338dbb3dd0d99b1b4
vulcan raven
  • 32,612
  • 11
  • 57
  • 93
  • 2
    It's absurd and ridiculous but exiftool can actually read the dotnet assembly info! It reads other properties as well. And `exiftool ./System.Private.CoreLib.dll` is much simpler than using monodis. Not to speak of microsofts own dotnet-ildasm. Cudos @vulcan-raven and to dthe devs of exiftool. – Michael Apr 12 '23 at 11:44
4

The ikdasm tool that is also distributed with Mono is more robust than monodis, which unfortunately crashes with the message "Segmentation fault: 11" on many DLL files. The maintainers explicitly recommend the use of ikdasm rather than monodis: https://github.com/mono/mono/issues/8900#issuecomment-428392112

Usage example (with an assembly that monodis currently cannot handle):

ikdasm -assembly System.Runtime.InteropServices.RuntimeInformation.dll | grep Version:
Otto G
  • 670
  • 7
  • 14
3

This is a really old question and just about everything has changed, but as of dotnet 2.1 (so you can dotnet tool install) you can install dotnet-ildasm:

dotnet tool install --tool-path . dotnet-ildasm

Then you can use this function:

function dll_version {
  local dll="$1"
  local version_line
  local version

  version_line="$(./dotnet-ildasm "$dll" | grep AssemblyFileVersionAttribute)"
  # Uses SerString format:
  #   01 00 is Prolog
  #   SZARRAY for NumElem
  #   version chars for Elem
  #   00 00 for NamedArgs
  # See:
  #   https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf#%5B%7B%22num%22%3A2917%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C87%2C321%2C0%5D
  [[ $version_line =~ \(\ 01\ 00\ [0123456789aAbBcCdDeEfF][0123456789aAbBcCdDeEfF]\ (.*)\ 00\ 00\ \)$ ]]
  dotcount=0
  for i in ${BASH_REMATCH[1]}; do
    if [[ $i =~ ^2[eE]$ ]]; then
      (( dotcount++ ))
    fi
    if (( dotcount == 3 )); then
      break
    fi
    echo -n -e "\u$i"
  done
}
Lucas
  • 14,227
  • 9
  • 74
  • 124
2

to return just the vesion using monodis, use this code

monodis --assembly LDAPService.dll | grep Version | awk -F ' ' '{print $2}'

Installation packages: https://command-not-found.com/monodis

For example in ubuntu

apt-get install mono-utils
Scholtz
  • 2,878
  • 2
  • 23
  • 23