gpt4 book ai didi

python - 从 Python 查找应用程序的版本?

转载 作者:行者123 更新时间:2023-12-01 06:17:48 24 4
gpt4 key购买 nike

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

我已经进行了相当多的谷歌搜索,但找不到任何真正值得的东西。我尝试使用 GetFileVersionInfo,并且似乎得到了随机的东西。

有什么想法吗?

编辑

叹息......

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

此外,这仅适用于 Windows。

最佳答案

如果您不想使用 pywin32 执行此操作,那么您当然可以使用 ctypes 执行此操作。

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

有一个old mailing list post那就是做你所要求的事情。不幸的是,我现在没有方便自己测试的 Windows 盒子。但如果它不起作用,它至少应该给你一个好的开始。

这是代码,以防 2006 年的文件有一天消失:

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')

--

好的 - 回到 Windows 盒子附近。现在已经实际尝试过这段代码了。 “为我工作”。

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

关于python - 从 Python 查找应用程序的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2270345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com