我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用win32api.GetFileVersionInfo()。
def get_file_ver(fname): # see: http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/file/tip/win32/Demos/getfilever.py result = [] try: ver_strings = ('ProductVersion', 'FileVersion') pairs = win32api.GetFileVersionInfo(fname, '\\VarFileInfo\\Translation') ## \VarFileInfo\Translation returns list of available (language, codepage) pairs that can be used to retreive string info ## any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle two are language/codepage pair returned from above for lang, codepage in pairs: #print 'lang: ', lang, 'codepage:', codepage for ver_string in ver_strings: str_info = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, ver_string) result.append('%s %s' % (ver_string, win32api.GetFileVersionInfo(fname, str_info).strip())) except: pass return result
def _get_version_number (filename): from win32api import GetFileVersionInfo, LOWORD, HIWORD try: info = GetFileVersionInfo (filename, "\\") ms = info['FileVersionMS'] ls = info['FileVersionLS'] return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls) except: return 0,0,0,0
def fileVersion(program=None): ver = '?' if program == None: check = sys.argv[0] else: s = program.rfind('.') if s > 0 and program[s:] == '.html': check = program elif s < len(program) - 4: check = program + sys.argv[0][sys.argv[0].rfind('.'):] else: check = program if check[-3:] == '.py': try: modtime = datetime.fromtimestamp(os.path.getmtime(check)) ver = '1.0.%04d.%d%02d' % (modtime.year, modtime.month, modtime.day) except: pass elif check[-5:] == '.html': try: modtime = datetime.fromtimestamp(os.path.getmtime(check)) ver = '1.0.%04d.%d%02d' % (modtime.year, modtime.month, modtime.day) except: pass else: if sys.platform == 'win32' or sys.platform == 'cygwin': try: if check.find('\\') >= 0: # if full path info = GetFileVersionInfo(check, '\\') else: info = GetFileVersionInfo(os.getcwd() + '\\' + check, '\\') ms = info['ProductVersionMS'] # ls = info['FileVersionLS'] ls = info['ProductVersionLS'] ver = str(HIWORD(ms)) + '.' + str(LOWORD(ms)) + '.' + str(HIWORD(ls)) + '.' + str(LOWORD(ls)) except: try: info = os.path.getmtime(os.getcwd() + '\\' + check) ver = '1.0.' + datetime.datetime.fromtimestamp(info).strftime('%Y.%m%d') if ver[9] == '0': ver = ver[:9] + ver[10:] except: pass return ver