2010-02-16 37 views
1

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

我已经做了一些Google搜索,找不到任何真正值得的东西。我尝试使用GetFileVersionInfo,并且我似乎得到了一个随机杂乱的东西。

任何想法?

编辑

唉....

原来pywin32并不总是安装在所有的机器。有谁知道它是否可能通过ctypes做同样的事情?

此外,这仅适用于Windows。

回答

2

如果你不想使用pywin32来做这件事,那么你肯定可以用ctypes做到这一点。

这个技巧将解码那个愚蠢的文件版本结构。

有一个old mailing list post正在做你在问什么。不幸的是,我现在没有一个方便自己测试的窗口框。但如果它不起作用,它至少应该给你一个好的开始。

下面的代码,在这些情况下,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) 
0

有一个名为“弦”,打印在任何文件中的可打印字符(二进制或非二进制),请尝试使用,并期待像在Windows模式

一个版本号的GNU Linux的工具,你可以得到这里的字符串http://unxutils.sourceforge.net/

相关问题