Inno Setup:验证是否安装了 .NET 4.0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4104011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inno Setup: Verify that .NET 4.0 is installed
提问by Mediator
I have a component that requires .NET 4.0 to run, how can my Inno Setup installer verify that it is installed, and if not, prompt the user to install it?
我有一个需要 .NET 4.0 才能运行的组件,我的 Inno Setup 安装程序如何验证它是否已安装,如果没有,提示用户安装它?
回答by Cody Gray
The InitializeSetupfunction is called when the Inno Setup executable is run. Inserting this code for a custom script should do what you want:
该InitializeSetup函数在 Inno Setup 可执行文件运行时被调用。为自定义脚本插入此代码应该可以执行您想要的操作:
function IsDotNetDetected(version: string; service: cardinal): boolean;
// Indicates whether the specified version and service pack of the .NET Framework is installed.
//
// version -- Specify one of these strings for the required .NET Framework version:
// 'v1.1' .NET Framework 1.1
// 'v2.0' .NET Framework 2.0
// 'v3.0' .NET Framework 3.0
// 'v3.5' .NET Framework 3.5
// 'v4\Client' .NET Framework 4.0 Client Profile
// 'v4\Full' .NET Framework 4.0 Full Installation
// 'v4.5' .NET Framework 4.5
// 'v4.5.1' .NET Framework 4.5.1
// 'v4.5.2' .NET Framework 4.5.2
// 'v4.6' .NET Framework 4.6
// 'v4.6.1' .NET Framework 4.6.1
// 'v4.6.2' .NET Framework 4.6.2
// 'v4.7' .NET Framework 4.7
//
// service -- Specify any non-negative integer for the required service pack level:
// 0 No service packs required
// 1, 2, etc. Service pack 1, 2, etc. required
var
key, versionKey: string;
install, release, serviceCount, versionRelease: cardinal;
success: boolean;
begin
versionKey := version;
versionRelease := 0;
// .NET 1.1 and 2.0 embed release number in version key
if version = 'v1.1' then begin
versionKey := 'v1.1.4322';
end else if version = 'v2.0' then begin
versionKey := 'v2.0.50727';
end
// .NET 4.5 and newer install as update to .NET 4.0 Full
else if Pos('v4.', version) = 1 then begin
versionKey := 'v4\Full';
case version of
'v4.5': versionRelease := 378389;
'v4.5.1': versionRelease := 378675; // 378758 on Windows 8 and older
'v4.5.2': versionRelease := 379893;
'v4.6': versionRelease := 393295; // 393297 on Windows 8.1 and older
'v4.6.1': versionRelease := 394254; // 394271 before Win10 November Update
'v4.6.2': versionRelease := 394802; // 394806 before Win10 Anniversary Update
'v4.7': versionRelease := 460798; // 460805 before Win10 Creators Update
end;
end;
// installation key group for all .NET versions
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\' + versionKey;
// .NET 3.0 uses value InstallSuccess in subkey Setup
if Pos('v3.0', version) = 1 then begin
success := RegQueryDWordValue(HKLM, key + '\Setup', 'InstallSuccess', install);
end else begin
success := RegQueryDWordValue(HKLM, key, 'Install', install);
end;
// .NET 4.0 and newer use value Servicing instead of SP
if Pos('v4', version) = 1 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Servicing', serviceCount);
end else begin
success := success and RegQueryDWordValue(HKLM, key, 'SP', serviceCount);
end;
// .NET 4.5 and newer use additional value Release
if versionRelease > 0 then begin
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
success := success and (release >= versionRelease);
end;
result := success and (install = 1) and (serviceCount >= service);
end;
function InitializeSetup(): Boolean;
begin
if not IsDotNetDetected('v4.6', 0) then begin
MsgBox('MyApp requires Microsoft .NET Framework 4.6.'#13#13
'Please use Windows Update to install this version,'#13
'and then re-run the MyApp setup program.', mbInformation, MB_OK);
result := false;
end else
result := true;
end;
(Code taken from here: http://www.kynosarges.de/DotNetVersion.html)
(代码取自此处:http: //www.kynosarges.de/DotNetVersion.html)
First, it checks for the presence of a registry entry that indicates the version of the .NET framework that is installed. If the registry entry is not present, it prompts the user to download the .NET framework. If the user says Yes, it opens the download URL. (You may have to change the version it specifies in the script to version 4.0.)
首先,它会检查是否存在指示安装的 .NET 框架版本的注册表项。如果注册表项不存在,它会提示用户下载 .NET 框架。如果用户说是,它会打开下载 URL。(您可能需要将它在脚本中指定的版本更改为 4.0 版。)
I also came across this article on CodeProject, which may be a more comprehensive and customizable way of doing what you're looking for, although it may take more work to understand and will have to be modified to work with version 4.0.
我还在CodeProject 上看到了这篇文章,这可能是一种更全面和可定制的方式来做你正在寻找的事情,尽管它可能需要更多的工作来理解并且必须修改才能使用 4.0 版。
回答by David Taylor
@Cody Gray - Thanks for your solution. Very helpful!
@Cody Gray - 感谢您的解决方案。很有帮助!
In case anyone is interested, here is my take on his function using enumeration values instead of strings. This change is purely a matter of personal preference. The code includes the changes for v4.5 and seems to work properly based on my limited testing.
如果有人感兴趣,这里是我对他使用枚举值而不是字符串的函数的看法。这种变化纯粹是个人喜好的问题。该代码包括 v4.5 的更改,并且根据我的有限测试似乎可以正常工作。
David
大卫
[Code]
//
// Enumeration used to specify a .NET framework version
//
type TDotNetFramework = (
DotNet_v11_4322, // .NET Framework 1.1
DotNet_v20_50727, // .NET Framework 2.0
DotNet_v30, // .NET Framework 3.0
DotNet_v35, // .NET Framework 3.5
DotNet_v4_Client, // .NET Framework 4.0 Client Profile
DotNet_v4_Full, // .NET Framework 4.0 Full Installation
DotNet_v45); // .NET Framework 4.5
//
// Checks whether the specified .NET Framework version and service pack
// is installed (See: http://www.kynosarges.de/DotNetVersion.html)
//
// Parameters:
// Version - Required .NET Framework version
// ServicePack - Required service pack level (0: None, 1: SP1, 2: SP2 etc.)
//
function IsDotNetInstalled(Version: TDotNetFramework; ServicePack: cardinal): boolean;
var
KeyName : string;
Check45 : boolean;
Success : boolean;
InstallFlag : cardinal;
ReleaseVer : cardinal;
ServiceCount : cardinal;
begin
// Registry path for the requested .NET Version
KeyName := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\';
case Version of
DotNet_v11_4322: KeyName := KeyName + 'v1.1.4322';
DotNet_v20_50727: KeyName := KeyName + 'v2.0.50727';
DotNet_v30: KeyName := KeyName + 'v3.0';
DotNet_v35: KeyName := KeyName + 'v3.5';
DotNet_v4_Client: KeyName := KeyName + 'v4\Client';
DotNet_v4_Full: KeyName := KeyName + 'v4\Full';
DotNet_v45: KeyName := KeyName + 'v4\Full';
end;
// .NET 3.0 uses "InstallSuccess" key in subkey Setup
if (Version = DotNet_v30) then
Success := RegQueryDWordValue(HKLM, KeyName + '\Setup', 'InstallSuccess', InstallFlag) else
Success := RegQueryDWordValue(HKLM, KeyName, 'Install', InstallFlag);
// .NET 4.0/4.5 uses "Servicing" key instead of "SP"
if (Version = DotNet_v4_Client) or
(Version = DotNet_v4_Full) or
(Version = DotNet_v45) then
Success := Success and RegQueryDWordValue(HKLM, KeyName, 'Servicing', ServiceCount) else
Success := Success and RegQueryDWordValue(HKLM, KeyName, 'SP', ServiceCount);
// .NET 4.5 is distinguished from .NET 4.0 by the Release key
if (Version = DotNet_v45) then
begin
Success := Success and RegQueryDWordValue(HKLM, KeyName, 'Release', ReleaseVer);
Success := Success and (ReleaseVer >= 378389);
end;
Result := Success and (InstallFlag = 1) and (ServiceCount >= ServicePack);
end;
回答by Tobias81
If you don't care if the "Full" or just the "Client" version of .NET 4 is installed:
如果您不关心是否安装了 .NET 4 的“完整”或“客户端”版本:
try
ExpandConstant('{dotnet40}');
// Installed
except
// Not installed
end;

