对于Windows EXE和DLL的:
require "Win32API" FILENAME = "c:/ruby/bin/ruby.exe" #your filename here s="" vsize=Win32API.new('version.dll','GetFileVersionInfoSize',['P','P'],'L').call(FILENAME,s) p vsize if (vsize > 0) result = ' '*vsize Win32API.new('version.dll','GetFileVersionInfo','L',vsize,result) rstring = result.unpack('v*').map{|s| s.chr if s<256}*'' r = /FileVersion..(.*?)\000/.match(rstring) puts "FileVersion = #{r ? r[1] : '??' }" else puts "No Version Info" end
“unpack”regexp部分是一个黑客,“正确”的方式是VerQueryValue API,但这应该适用于大多数文件. (可能在扩展字符集上可能失败)