获取.exe的版本信息 [英] Get Version Info for .exe

查看:91
本文介绍了获取.exe的版本信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何通过Java获取可执行文件的文件信息。方案是我的本地系统上有一个文件,如果服务器上的版本比较新,那么我的系统上的版本需要从服务器下载文件。

Does anybody know how got get the version info for a executable/file via Java. The scenario is that I have a file on my local system and if the version on the server is newer then the one on my system I need to download the file from the server.

推荐答案

在线上和编码花了数小时后,我找到了一个使用JNA获取文件版本信息的解决方案。

After spending hours online and coding I found a solution using JNA to get the Version Information for a file.

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.W32APIOptions;
import java.io.IOException;


public class FileVersionInfo
{
    interface Version extends Library {

        Version INSTANCE = (Version) Native.loadLibrary("Version", Version.class, W32APIOptions.UNICODE_OPTIONS);

        public int GetFileVersionInfoSizeW(String lptstrFilename, int dwDummy);

        public boolean GetFileVersionInfoW(String lptstrFilename, int dwHandle,
            int dwLen, Pointer lpData);

        public int VerQueryValueW(Pointer pBlock, String lpSubBlock,
            PointerByReference lplpBuffer, IntByReference puLen);

    }

    static class VS_FIXEDFILEINFO extends com.sun.jna.Structure {
        public int dwSignature;
        public int dwStrucVersion;
        public int dwFileVersionMS;
        public int dwFileVersionLS;
        public int dwProductVersionMS;
        public int dwProductVersionLS;
        public int dwFileFlagsMask;
        public int dwFileFlags;
        public int dwFileOS;
        public int dwFileType;
        public int dwFileSubtype;
        public int dwFileDateMS;
        public int dwFileDateLS;

           public VS_FIXEDFILEINFO(com.sun.jna.Pointer p){
                super(p);
           }
    }
    public static void main(String[] args) throws IOException {

        int dwDummy = 0;
        int versionlength = Version.INSTANCE.GetFileVersionInfoSizeW(
                "C:\\Test\\chromeinstall.exe", dwDummy);

        byte[] bufferarray = new byte[versionlength];
        Pointer lpData = new Memory(bufferarray.length);    

        PointerByReference lplpBuffer = new PointerByReference();
        IntByReference puLen = new IntByReference();
        boolean FileInfoResult = Version.INSTANCE.GetFileVersionInfoW(
                "C:\\Test\\chromeinstall.exe",
                0, versionlength, lpData);
        System.out.println(FileInfoResult);
        int verQueryVal = Version.INSTANCE.VerQueryValueW(lpData,
                "\\", lplpBuffer,
                puLen);

        VS_FIXEDFILEINFO lplpBufStructure = new VS_FIXEDFILEINFO(
                lplpBuffer.getValue());
        lplpBufStructure.read();

        short[] rtnData = new short[4];
        rtnData[0] = (short) (lplpBufStructure.dwFileVersionMS >> 16);
        rtnData[1] = (short) (lplpBufStructure.dwFileVersionMS & 0xffff);
        rtnData[2] = (short) (lplpBufStructure.dwFileVersionLS >> 16);
        rtnData[3] = (short) (lplpBufStructure.dwFileVersionLS & 0xffff);

        for (int i = 0; i < rtnData.length; i++) {
            System.out.println(rtnData[i]);
        }

} 

这篇关于获取.exe的版本信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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