一個經典的Delphi獲取應用程序和文件版本號的程序代碼,函數名稱:GetFileVersion(),用法格式:GetFileVersion(FileName: string),括號內請輸入需要獲取程序或文件的路徑信息,若程序無版本信息,則返回值為空:
01
function GetAppVersion:String;
02
function GetFileVersion(FileName:
string
):
string
;
03
type
04
PVerInfo = ^TVS_FIXEDFILEINFO;
05
TVS_FIXEDFILEINFO = record
06
dwSignature: longint;
07
dwStrucVersion: longint;
08
dwFileVersionMS: longint;
09
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;
51
begin
52
Result:=GetFileVersion(Application.ExeName);
53
end;
這個代碼有些老,不過仍然可以用,程序在執行時,如有版本號,返回值為版本號的值,否則返回值為空,返回值的格式為如1.0.0.0。