在 Python 中获取 exe 的完整文件版本

Grabbing full file version of an exe in Python

我想知道是否有一种简单的方法可以在 python 中获取 exe 的完整文件版本(例如,如果您右键单击文件的属性并转到“详细信息”,您会发现类似文件版本 1.1.1.0).

我通过使用 win32api.GetFileVersionInfo 找到了一些接近的东西,但是在列出文件属性时,在 FileVersionLS 下它似乎只给出了一位数字,即文件版本的最右边数字(所以在在上面的示例中,当我需要整个版本号时,它在 1.1.1.0 中给出了 0。

希望这是有道理的,如果有什么我需要详细说明的,请告诉我。

感谢阅读!

FileVersionLSFileVersionMS共同作用,将完整的版本号表示为64位整数:

dwFileVersionMS

Type: DWORD

The most significant 32 bits of the file's binary version number. This member is used with dwFileVersionLS to form a 64-bit value used for numeric comparisons.

dwFileVersionLS

Type: DWORD

The least significant 32 bits of the file's binary version number. This member is used with dwFileVersionMS to form a 64-bit value used for numeric comparisons.

它们每个都由两个 16 位数字组成,因此您必须对各个数字进行位移。您可以为此使用 win32api.LOWORD()win32api.HIWORD(),例如:

def get_file_version(self, path):
    info = win32api.GetFileVersionInfo(path, '\')
    ms = info['FileVersionMS']
    ls = info['FileVersionLS']
    return (win32api.HIWORD(ms), win32api.LOWORD(ms),
            win32api.HIWORD(ls), win32api.LOWORD(ls))