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

房东的猫 提交于 2019-12-10 12:28:56

问题


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?


回答1:


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.




回答2:


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

monodis --assembly file.exe | grep Version



回答3:


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:



回答4:


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
}


来源:https://stackoverflow.com/questions/3946368/how-to-get-the-assemblyversion-of-a-net-file-in-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!