• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi获取指定文件的版本号

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

获取指定文件的版本号

方式一:

function GetFileVersion(FileName: string): string; 
   type 
     PVerInfo = ^TVS_FIXEDFILEINFO; 
     TVS_FIXEDFILEINFO = record 
       dwSignature: longint; 
       dwStrucVersion: longint; 
       dwFileVersionMS: longint; 
       dwFileVersionLS: longint; 
       dwFileFlagsMask: longint; 
       dwFileFlags: longint; 
       dwFileOS: longint; 
       dwFileType: longint; 
       dwFileSubtype: longint; 
       dwFileDateMS: longint; 
       dwFileDateLS: longint; 
     end; 
   var 
     ExeNames: array[0..255] of char; 
     zKeyPath: array[0..255] of Char; 
     VerInfo: PVerInfo; 
     Buf: pointer; 
     Sz: word; 
     L, Len: Cardinal; 
   begin 
     StrPCopy(ExeNames, FileName); 
     Sz := GetFileVersionInfoSize(ExeNames, L); 
     if Sz=0 then 
     begin 
       Result:=''; 
       Exit; 
     end; 

     try 
       GetMem(Buf, Sz); 
       try 
         GetFileVersionInfo(ExeNames, 0, Sz, Buf); 
         if VerQueryValue(Buf, '\', Pointer(VerInfo), Len) then 
         begin 
           Result := IntToStr(HIWORD(VerInfo.dwFileVersionMS)) + '.' + 
                     IntToStr(LOWORD(VerInfo.dwFileVersionMS)) + '.' + 
                     IntToStr(HIWORD(VerInfo.dwFileVersionLS)) + '.' + 
                     IntToStr(LOWORD(VerInfo.dwFileVersionLS)); 

         end; 
       finally 
         FreeMem(Buf); 
       end; 
     except 
       Result := '-1'; 
     end; 
   end;

方式二:

function GetFileVersion(AFileName: string): string;
var
n, Len: DWORD;
Buf : PChar;
Value: Pointer;
szName: array [0..255] of Char;
Transstring: string;
begin
Len := GetFileVersionInfoSize(PChar(AFileName), n);
if Len > 0 then
begin
    Buf := AllocMem(Len);
    if GetFileVersionInfo(Pchar(AFileName), n, Len, Buf) then
    begin
      Value := nil;
      VerQueryValue(Buf, '\VarFileInfo\Translation', Value, Len);
      if Value <> nil then
        Transstring := IntToHex(MakeLong(HiWord(LongInt(Value^)),
          LoWord(LongInt(Value^))),8);
      StrPCopy(szName, '\stringFileInfo\' + Transstring + '\FileVersion');
      if VerQueryValue(Buf, szName, Value, Len) then
        Result := StrPas(Pchar(Value));
    end;
    FreeMem(Buf, n);
end;
end; 

 

按钮 代码示例:

procedure TForm1.Button1Click(Sender: TObject); 
var 
dwVerInfoSize, dwVerHnd: DWORD; 
hMem: THandle; 
lpvMem, lszVer: Pointer; 
szGetName: array[0..255] of Char; 
cchVer: UINT; 
fRet: Boolean; 
vsffi: VS_FIXEDFILEINFO; 
begin 
dwVerInfoSize := GetFileVersionInfoSize('Shell32.dll', dwVerHnd); 
if Boolean(dwVerInfoSize) then 
begin 
hMem := GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize); 
lpvMem := GlobalLock(hMem); 
GetFileVersionInfo('Shell32.dll', dwVerHnd, dwVerInfoSize, lpvMem); 
StrCopy(szGetName, '\'); 
lszVer := nil; 
fRet := VerQueryValue(lpvMem, szGetName, lszVer, cchVer); 
if fRet and Boolean(cchVer) and Boolean(lszVer) then 
begin 
vsffi := VS_FIXEDFILEINFO(lszVer^); 
ShowMessage('文件版本: ' + 
IntToStr(HIWORD(vsffi.dwFileVersionMS)) + '.' + 
IntToStr(LOWORD(vsffi.dwFileVersionMS)) + '.' + 
IntToStr(HIWORD(vsffi.dwFileVersionLS)) + '.' + 
IntToStr(LOWORD(vsffi.dwFileVersionLS))); 
end; 
GlobalUnlock(hMem); 
GlobalFree(hMem); 
end; 
end;

  

方式3:

function GetFileVersion(fn: string; var ma, mi, r ,b: integer):boolean;//得到本程序的版本号 
var 
buf, p: pChar; 
sver: ^VS_FIXEDFILEINFO ; 
i: LongWord; 
begin 
i:= GetFileVersionInfoSize(pchar(fn), i); 
new(sver); 
p:= pchar(sver); 
GetMem(buf, i); 
ZeroMemory(buf, i); 
result:= false; 
if GetFileVersionInfo(pchar(fn), 0, 4096, pointer(buf)) then 
if VerQueryValue(buf, '\', pointer(sver), i) then begin 
ma:= sVer^.dwFileVersionMS shr 16; 
mi:= sver^.dwFileVersionMS and $0000ffff; 
r:= sver^.dwFileVersionLS shr 16; 
b:= sver^.dwFileVersionLS and $0000ffff; 
result:= true; 
end; 
Dispose(p); 
FreeMem(buf); 
end;

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
DelphiXE6使用定时器或者线程解决程序界面无响应问题发布时间:2022-07-18
下一篇:
Delphi读写UTF-8、Unicode格式文本文件发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap