需要Uses

 SysUtils,windows,Classes;



1 /// <summary>
2 /// <remarks>
3 /// 获取可执行文件的版本号;
4 /// </remarks>
5 /// <param name="sFileName">文件全称(要带上路径)</param>
6 /// </summary>
7 /// <returns>String</returns>
8 function GetFileVersionNO(sFileName:string):string;
9 /// <summary>
10 /// <remarks>
11 /// 获取DLL文件的版本号
12 /// 可以传递多个文件名,文件名之间用英文下的逗号(,)隔开
13 /// </remarks>
14 /// <param name="sFileName">文件名(不要带路径)</param>
15 /// <param name="sReturnValueSeparator">返回值的版本号分隔符(默认为英文状态下的逗号)</param>
16 /// </summary>
17 /// <returns>String</returns>
18 function GetDLLVersionNOs(sFileNames:string; sReturnValueSeparator:String=','):string;
19
20
21
22
23 function GetFileVersionNO(sFileName:string):string;
24 var
25 VerInfoSize,VerValueSize,Dummy:DWORD;
26 VerInfo: Pointer;
27 VerValue: PVSFixedFileInfo;
28 Ver1,Ver2,Ver3,Ver4:Word;
29 begin
30 Result:='';
31 if not FileExists(sFileName) then
32 Exit;
33 VerInfoSize:=GetFileVersionInfoSize(PChar(sFileName),Dummy);
34 if VerInfoSize=0 then
35 exit;
36 GetMem(VerInfo,VerInfoSize);
37 if not GetFileVersionInfo(PChar(sFileName),0,VerInfoSize,VerInfo) then
38 exit;
39 VerQueryValue(VerInfo, '\', Pointer(VerValue), VerValueSize);
40 with VerValue^ do
41 begin
42 Ver1:=dwFileVersionMS shr 16;
43 Ver2:=dwFileVersionMS and $FFFF;
44 Ver3:=dwFileVersionLS shr 16;
45 Ver4:=dwFileVersionLS and $FFFF;
46 Result:=inttostr(ver1)+'.'+inttostr(ver2)+'.'+inttostr(ver3)+'.'+inttostr(ver4);
47 end;
48 FreeMem(VerInfo,VerInfoSize);
49 end;
50
51 function GetDLLVersionNOs(sFileNames:string; sReturnValueSeparator:String=','):string;
52 var
53 vList:TStringList;
54 i:Integer;
55 vFrontPartPath,vFullPath, VerNo:string;
56 begin
57 Result:='';
58 if sFileNames='' then
59 Exit;
60 if sReturnValueSeparator='' then
61 Exit;
62 if sReturnValueSeparator = '.'then //不允许设置点的分隔符
63 Exit;
64 if sReturnValueSeparator = ':'then //不允许设置点的分隔符
65 Exit;
66 vFrontPartPath:=ExtractFilePath(ParamStr(0));//Application.ExeName //默认 目标文件都和程序在一个文件夹中
67 try
68 vList:=TStringList.Create;
69 vList.Delimiter:=',';
70 vList.DelimitedText:=sFileNames;
71 for i:=0 to vList.Count - 1 do
72 begin
73 vFullPath:=vFrontPartPath+vList.Strings[i]+'.dll';
74 VerNo:=GetFileVersionNO(vFullPath);
75 if VerNo='' then
76 VerNo:='NaN';
77
78 Result:=Result+sReturnValueSeparator+vList.Strings[i]+'.dll Version Number: '+VerNo;
79 end;
80 if Result<>'' then
81 begin
82 Delete(Result,1,Length(sReturnValueSeparator));
83 end;
84 finally
85 vList.Clear;
86 vList.Free;
87 end;
88 end;