翻譯自: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;