开发者

How to get the AssemblyVersion of a .Net file in Linux

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 informati开发者_StackOverflow中文版on.

Any ideas?


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.


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

monodis --assembly file.exe | grep Version


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:


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
}


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜