delphi 获取版本号、格式化版本信息、比较版本号等相关操作  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 获取版本号、格式化版本信息、比较版本号等相关操作


  1. // 获取版本号
  2. function GetFileVersion(FileName: string): string;
  3. type
  4. PVerInfo = ^TVS_FIXEDFILEINFO;
  5. TVS_FIXEDFILEINFO = record
  6. dwSignature: longint;
  7. dwStrucVersion: longint;
  8. dwFileVersionMS: longint;
  9. dwFileVersionLS: longint;
  10. dwFileFlagsMask: longint;
  11. dwFileFlags: longint;
  12. dwFileOS: longint;
  13. dwFileType: longint;
  14. dwFileSubtype: longint;
  15. dwFileDateMS: longint;
  16. dwFileDateLS: longint;
  17. end;
  18. var
  19. ExeNames: array[0..255] of char;
  20. zKeyPath: array[0..255] of Char;
  21. VerInfo: PVerInfo;
  22. Buf: pointer;
  23. Sz: word;
  24. L, Len: Cardinal;
  25. begin
  26. StrPCopy(ExeNames, FileName);
  27. Sz := GetFileVersionInfoSize(ExeNames, L);
  28. if Sz = 0 then
  29. begin
  30. Result := '';
  31. Exit;
  32. end;
  33. try
  34. GetMem(Buf, Sz);
  35. try
  36. GetFileVersionInfo(ExeNames, 0, Sz, Buf);
  37. if VerQueryValue(Buf, '\', Pointer(VerInfo), Len) then
  38. begin
  39. Result := IntToStr(HIWORD(VerInfo.dwFileVersionMS)) + '.' +
  40. IntToStr(LOWORD(VerInfo.dwFileVersionMS)) + '.' +
  41. IntToStr(HIWORD(VerInfo.dwFileVersionLS)) + '.' +
  42. IntToStr(LOWORD(VerInfo.dwFileVersionLS));
  43. end;
  44. finally
  45. FreeMem(Buf);
  46. end;
  47. except
  48. Result := '-1';
  49. end;
  50. end;

// 获取版本号 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;

以上获取版本号操作转自网上

  1. // 版本号比较{返回版本差 版本号格式:1.0.0.1}
  2. function CompareVersion(VersionA, VersionB: string): string;
  3. var
  4. listA : TStringList;
  5. listB : TStringList;
  6. i : Integer;
  7. strCompare : string;
  8. begin
  9. Result := '';
  10. // 创建
  11. listA := TStringList.Create();
  12. listB := TStringList.Create();
  13. // 获取列表
  14. ExtractStrings(['.'], [' '], PChar(VersionA), listA);
  15. ExtractStrings(['.'], [' '], PChar(VersionB), listB);
  16. if listA.Count <> listB.Count then
  17. Exit;
  18. // 比较
  19. for i := 0 to listA.Count - 2 do
  20. begin
  21. strCompare := strCompare + IntToStr(StrToInt(listA[i]) - StrToInt(listB[i])) + '.';
  22. end;
  23. i := listA.Count - 1;
  24. if i < 0 then
  25. Exit;
  26. strCompare := strCompare + IntToStr(StrToInt(listA[i]) - StrToInt(listB[i]));
  27. // 释放
  28. if Assigned(listA) then
  29. FreeAndNil(listA);
  30. if Assigned(listB) then
  31. FreeAndNil(listB);
  32. Result := strCompare;
  33. end;

// 版本号比较{返回版本差 版本号格式:1.0.0.1} function CompareVersion(VersionA, VersionB: string): string; var listA : TStringList; listB : TStringList; i : Integer; strCompare : string; begin Result := ''; // 创建 listA := TStringList.Create(); listB := TStringList.Create(); // 获取列表 ExtractStrings(['.'], [' '], PChar(VersionA), listA); ExtractStrings(['.'], [' '], PChar(VersionB), listB); if listA.Count <> listB.Count then Exit; // 比较 for i := 0 to listA.Count - 2 do begin strCompare := strCompare + IntToStr(StrToInt(listA[i]) - StrToInt(listB[i])) + '.'; end; i := listA.Count - 1; if i < 0 then Exit; strCompare := strCompare + IntToStr(StrToInt(listA[i]) - StrToInt(listB[i])); // 释放 if Assigned(listA) then FreeAndNil(listA); if Assigned(listB) then FreeAndNil(listB); Result := strCompare; end;

  1. // 版本号转换为整数(和计算方式)
  2. function VersionSumToInt(Version: string): Integer;
  3. var
  4. list : TStringList;
  5. i : Integer;
  6. nSum : Integer;
  7. begin
  8. Result := -1;
  9. nSum := 0;
  10. list := TStringList.Create();
  11. ExtractStrings(['.'], [' '], PChar(Version), list);
  12. for i := 0 to list.Count - 1 do
  13. begin
  14. if StrToIntDef(list[i], -1) < 0 then
  15. Exit;
  16. nSum := nSum + StrToInt(list[i]);
  17. end;
  18. if Assigned(list) then
  19. FreeAndNil(list);
  20. Result := nSum;
  21. end;

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.040642976760864 seconds