我有一个组件需要.NET 4.0才能运行,我的Inno Setup安装程序如何验证它是否已安装,如果没有,则提示用户安装?
我有一个需要.NET 4.0才能运行的组件,如何在Inno Setup安装程序中检查其是否已经安装,并在未安装时提示用户进行安装。
我有一个组件需要.NET 4.0才能运行,我的Inno Setup安装程序如何验证它是否已安装,如果没有,则提示用户安装?
我有一个需要.NET 4.0才能运行的组件,如何在Inno Setup安装程序中检查其是否已经安装,并在未安装时提示用户进行安装。
当Inno Setup可执行文件运行时,将调用InitializeSetup
函数。为自定义脚本插入此代码应该可以满足您的需求:
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
// 'v4.7.1' .NET Framework 4.7.1
// 'v4.7.2' .NET Framework 4.7.2
// 'v4.8' .NET Framework 4.8
//
// 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
'v4.7.1': versionRelease := 461308; // 461310 before Win10 Fall Creators Update
'v4.7.2': versionRelease := 461808; // 461814 before Win10 April 2018 Update
'v4.8': versionRelease := 528040; // 528049 before Win10 May 2019 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;
(代码来源: http://www.kynosarges.de/DotNetVersion.html)
首先,它检查注册表中是否存在指示已安装的.NET框架版本的条目。如果注册表条目不存在,则提示用户下载.NET框架。如果用户选择是,则会打开下载网址。(您可能需要更改脚本中指定的版本为4.0版本。)
@Cody Gray - 感谢您的解决方案。非常有帮助!
如果有人感兴趣,这是我对他的函数的看法,使用枚举值而不是字符串。这种更改纯粹是个人偏好的问题。该代码包括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;
function IsDotNetReallyInstalled(): Boolean;
begin
result := IsDotNetInstalled(DotNet_v45, 0);
end;
然后在您的“check”函数中调用它。 - Robert Koernke我发现自从innosetup版本6.0.5起,该功能已经成为框架的一部分。因此,您不必自己编写该函数。
函数: IsDotNetInstalled
这是一个简短的例子:
[Setup]
AppName=".NET Framwork Test"
AppVersion=0.0.0.1
DefaultDirName={tmp}\MyApp
[Code]
function InitializeSetup(): Boolean;
begin
Result := IsDotNetInstalled(net45, 0);
if Result then begin
MsgBox('Min .NET Framwork is installed', mbInformation, MB_OK);
end else begin
MsgBox('Min .NET Framwork is not installed', mbInformation, MB_OK);
end;
end;
try
ExpandConstant('{dotnet40}');
// Installed
except
// Not installed
end;
感谢大家提供的已有解决方案,它们效果非常好。
不过我只需要支持一种框架版本,且不需要支持“古老”的版本(.NET 4.0或更早版本)。我也不需要检查补丁版本。
所以,代码变得简单了很多,我觉得这样减少了噪音。
[Code]
// http://www.kynosarges.de/DotNetVersion.html
function IsDotNetDetected(): boolean;
var
key: string;
install, release: cardinal;
success: boolean;
begin
key := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full'
// success: true if the registry has been read successfully
success := RegQueryDWordValue(HKLM, key, 'Install', install);
success := success and RegQueryDWordValue(HKLM, key, 'Release', release);
// install = 1 if framework is installed
// 461808 -> .NET 4.7.2 461814 before Win10 April 2018 Update
// see https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
// for details
result := success and (install = 1) and (release >= 461808);
end;
function InitializeSetup(): Boolean;
begin
if not IsDotNetDetected() then begin
MsgBox('MyApp requires Microsoft .NET Framework 4.7.2.'#13#13
'Please install it and then re-run this setup program.', mbInformation, MB_OK);
result := false;
end else
result := true;
end;
你只需要在上述代码中用来表示版本的常量(461808
)替换为在Microsoft参考页面中所需的值,并相应更新错误消息。
InitializeSetup
事件中运行框架设置,这会在运行安装向导时立即开始安装框架,这对用户体验是错误的。在您的[Run]
部分中使用Check
条件参数,就像这样
。 - TLama