Inno Setup 检测已安装的.NET Framework 版本

2018-06-17 18:58:20来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

翻译自:http://kynosarges.org/DotNetVersion.html

由 Jordan Russell 写的 Inno Setup 是一个伟大的安装脚本程序,但缺乏一个内置的函数来确定安装在目标机器上的.NET Framework版本。幸运的是,它很容易使用 Pascal 脚本语言来实现这样的功能。

脚本

在下面的 Inno Setup的 脚本代码块,函数 IsDotNetDetected 检查指定的.NET Framework 版本和至少指定的服务包级别是否被安装了。所有列出的版本字符串是最终的发行版本; 测试版和发布候选通常具有不同的版本号。函数 InitializeSetup 演示了如何使用 IsDotNetDetected 检查 .NET Framework 4.6 是否有服务包。

我把在公共域代码这个小一点,这样你可以将它嵌入到自己的项目,修改和重新发布它,你认为合适。

 1 [Code]
 2 function IsDotNetDetected(version: string; service: cardinal): boolean;
 3 // Indicates whether the specified version and service pack of the .NET Framework is installed.
 4 //
 5 // version -- Specify one of these strings for the required .NET Framework version:
 6 //    'v1.1'          .NET Framework 1.1
 7 //    'v2.0'          .NET Framework 2.0
 8 //    'v3.0'          .NET Framework 3.0
 9 //    'v3.5'          .NET Framework 3.5
10 //    'v4\Client'     .NET Framework 4.0 Client Profile
11 //    'v4\Full'       .NET Framework 4.0 Full Installation
12 //    'v4.5'          .NET Framework 4.5
13 //    'v4.5.1'        .NET Framework 4.5.1
14 //    'v4.5.2'        .NET Framework 4.5.2
15 //    'v4.6'          .NET Framework 4.6
16 //    'v4.6.1'        .NET Framework 4.6.1
17 //    'v4.6.2'        .NET Framework 4.6.2
18 //
19 // service -- Specify any non-negative integer for the required service pack level:
20 //    0               No service packs required
21 //    1, 2, etc.      Service pack 1, 2, etc. required
22 var
23     key, versionKey: string;
24     install, release, serviceCount, versionRelease: cardinal;
25     success: boolean;
26 begin
27     versionKey := version;
28     versionRelease := 0;
29 
30     // .NET 1.1 and 2.0 embed release number in version key
31     if version = 'v1.1' then begin
32         versionKey := 'v1.1.4322';
33     end else if version = 'v2.0' then begin
34         versionKey := 'v2.0.50727';
35     end
36 
37     // .NET 4.5 and newer install as update to .NET 4.0 Full
38     else if Pos('v4.', version) = 1 then begin
39         versionKey := 'v4\Full';
40         case version of
41           'v4.5':   versionRelease := 378389;
42           'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
43           'v4.5.2': versionRelease := 379893;
44           'v4.6':   versionRelease := 393295; // 393297 on Windows 8.1 and older
45           'v4.6.1': versionRelease := 394254; // 394271 on Windows 8.1 and older
46           'v4.6.2': versionRelease := 394802; // 394806 on Windows 8.1 and older
47         end;
48     end;
49 
50     // installation key group for all .NET versions
51     key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;
52 
53     // .NET 3.0 uses value InstallSuccess in subkey Setup
54     if Pos('v3.0', version) = 1 then begin
55         success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
56     end else begin
57         success := RegQueryDWordValue(HKLM, key, 'Install', install);
58     end;
59 
60     // .NET 4.0 and newer use value Servicing instead of SP
61     if Pos('v4', version) = 1 then begin
62         success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
63     end else begin
64         success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
65     end;
66 
67     // .NET 4.5 and newer use additional value Release
68     if versionRelease > 0 then begin
69         success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
70         success := success and (release >= versionRelease);
71     end;
72 
73     result := success and (install = 1) and (serviceCount >= service);
74 end;
75 
76 
77 function InitializeSetup(): Boolean;
78 begin
79     if not IsDotNetDetected('v4.6', 0) then begin
80         MsgBox('MyApp requires Microsoft .NET Framework 4.6.'#13#13
81             'Please use Windows Update to install this version,'#13
82             'and then re-run the MyApp setup program.', mbInformation, MB_OK);
83         result := false;
84     end else
85         result := true;
86 end;

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:delphi7 写的一个验证btcdice网站结果的程序

下一篇:RemObjects 客户端完整执行流程