从 Python 中查找应用程序的版本? [英] Finding the version of an application from Python?

查看:30
本文介绍了从 Python 中查找应用程序的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我试图找出用户当前安装的 ArcGIS 版本,我查看了注册表,但找不到与版本字符串相关的任何内容.但是我知道它存储在 .exe 中.

我在谷歌上搜索了很多,但找不到任何真正值得的东西.我尝试使用 GetFileVersionInfo,但似乎随机混杂了一些东西.

有什么想法吗?

编辑

叹息....

结果 pywin32 并不总是安装在所有机器上.有谁知道是否可以通过 ctypes 做同样的事情?

这也仅适用于 Windows.

解决方案

如果您不想使用 pywin32 执行此操作,您肯定可以使用 ctypes 执行此操作.

诀窍是解码返回的那个愚蠢的文件版本结构.

有一个旧邮件列表帖子正在做你问的事情.不幸的是,我现在没有一个方便的 Windows 框来自己测试这个.但如果它不起作用,它至少应该给你一个好的开始.

这是代码,以防 2006 年的档案在某个时候消失:

导入数组从 ctypes 导入 *def get_file_info(文件名,信息):"""从文件中提取信息."""# 获取缓冲区所需的大小(如果没有信息,则为 0)大小 = windll.version.GetFileVersionInfoSizeA(文件名,无)# 如果文件中没有信息 ->空字符串如果不是尺寸:返回 '​​'# 创建缓冲区res = create_string_buffer(大小)# 加载文件信息到缓冲区 reswindll.version.GetFileVersionInfoA(文件名,无,大小,资源)r = c_uint()l = c_uint()# 查找代码页windll.version.VerQueryValueA(res, '\\VarFileInfo\\Translation',byref(r), byref(l))# 如果没有代码页 ->空字符串如果不是 l.value:返回 '​​'# 获取第一个代码页(还有什么?)codepages = array.array('H', string_at(r.value, l.value))codepage = tuple(codepages[:2].tolist())# 提取信息windll.version.VerQueryValueA(res, ('\\StringFileInfo\\%04x%04x\\'+ info) % 代码页、byref(r)、byref(l))返回 string_at(r.value, l.value)打印 get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')

--

好的 - 回到窗户附近.现在实际上已经尝试过这段代码.对我有用".

<预><代码>>>>打印 get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')6.1.7600.16385 (win7_rtm.090713-1255)

Basically i am trying to find out what version of ArcGIS the user currently has installed, i looked through the registry and couldn't find anything related to a version string. However i know it is stored, within the .exe.

I've done a fair bit of googling, and can't find anything really worth it. I tried using the GetFileVersionInfo, and i seem to get a random mishmash of stuff.

Any ideas?

EDIT

Sigh....

Turns out pywin32 is not always installed on all machines. Does anyone know if its possible to do the same thing via ctypes?

Also this is only for windows.

解决方案

If you prefer not to do this using pywin32, you would be able to do this with ctypes, for sure.

The trick will be decoding that silly file version structure that comes back.

There's one old mailing list post that is doing what you're asking. Unfortunately, I don't have a windows box handy to test this myself, right now. But if it doesn't work, it should at least give you a good start.

Here's the code, in case those 2006 archives vanish sometime:

import array
from ctypes import *

def get_file_info(filename, info):
    """
    Extract information from a file.
    """
    # Get size needed for buffer (0 if no info)
    size = windll.version.GetFileVersionInfoSizeA(filename, None)
    # If no info in file -> empty string
    if not size:
        return ''
    # Create buffer
    res = create_string_buffer(size)
    # Load file informations into buffer res
    windll.version.GetFileVersionInfoA(filename, None, size, res)
    r = c_uint()
    l = c_uint()
    # Look for codepages
    windll.version.VerQueryValueA(res, '\\VarFileInfo\\Translation',
                                  byref(r), byref(l))
    # If no codepage -> empty string
    if not l.value:
        return ''
    # Take the first codepage (what else ?)
    codepages = array.array('H', string_at(r.value, l.value))
    codepage = tuple(codepages[:2].tolist())
    # Extract information
    windll.version.VerQueryValueA(res, ('\\StringFileInfo\\%04x%04x\\'
+ info) % codepage, byref(r), byref(l))
    return string_at(r.value, l.value)

print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')

--

Ok - back near a windows box. Have actually tried this code now. "Works for me".

>>> print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')
6.1.7600.16385 (win7_rtm.090713-1255)

这篇关于从 Python 中查找应用程序的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆