在我们使用软件的时候,我们可能需要用到软件的版本号,但是如何获取软件版本号呢?在获取时我们可能会看到文件版本号和产品版本号,这两个大多时间是一样的,所以在使用的时候我们需要根据自己需要进行获取,以下将用python分别自动获取他们:
自动检测软件的文件版本号
import win32api
def get_exe_file_version(file_path):
try:
info = win32api.GetFileVersionInfo(file_path, '\\')
ms = info['FileVersionMS']
ls = info['FileVersionLS']
return f"{ms >> 16}.{ms & 0xFFFF}.{ls >> 16}.{ls & 0xFFFF}"
except Exception as e:
return f"Error: {e}"
file_path = r"C:\Program Files\YourSoftware\YourSoftware.exe" # 替换为你需要上传的文件路径
print(get_exe_file_version(file_path))
自动检测软件的产品版本号
import pefile
def get_exe_product_version(file_path):
try:
pe = pefile.PE(file_path)
for file_info in pe.FileInfo:
for entry in file_info:
if entry.Key.decode() == "StringFileInfo":
for st in entry.StringTable:
for key, value in st.entries.items():
if key.decode() == "ProductVersion":
return value.decode()
return "No ProductVersion found"
except Exception as e:
return f"Error: {e}"
path = r"C:\Program Files\YourSoftware\YourSoftware.exe" # 替换为你需要上传的文件路径
ver = get_exe_product_version(path)
print(ver)
如果大家还有什么简单的获取方法,还请大家勇于分享。