如何在Inno Setup中检测.NET 8桌面运行时是否已安装?
检测已安装的.NET 8 Desktop Runtime并跳过安装(Inno Setup实现)
可以通过读取Windows注册表来检测.NET 8 Desktop Runtime是否已安装,以下是具体实现方案:
检测逻辑说明
.NET Desktop Runtime的安装信息会写入以下注册表路径:
- 64位系统的64位运行时:
HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v8.0\DesktopRuntime - 64位系统的32位运行时:
HKLM\SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v8.0\DesktopRuntime - 32位系统:
HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v8.0\DesktopRuntime
我们可以读取这些路径下的Version键值,判断是否已安装等于或高于目标版本(8.0.3)的运行时。
修改后的完整代码
function CompareVersion(Ver1, Ver2: string): Integer; var V1Parts, V2Parts: TArrayOfString; I, Num1, Num2: Integer; begin Result := 0; SplitString(Ver1, '.', V1Parts); SplitString(Ver2, '.', V2Parts); for I := 0 to Max(Length(V1Parts), Length(V2Parts)) - 1 do begin if I < Length(V1Parts) then Num1 := StrToIntDef(V1Parts[I], 0) else Num1 := 0; if I < Length(V2Parts) then Num2 := StrToIntDef(V2Parts[I], 0) else Num2 := 0; if Num1 > Num2 then begin Result := 1; Exit; end else if Num1 < Num2 then begin Result := -1; Exit; end; end; end; function IsNet8DesktopRuntimeInstalled(TargetVersion: string): Boolean; var RegPath: string; InstalledVersion: string; begin Result := False; // 检查64位运行时(64位系统) RegPath := 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v8.0\DesktopRuntime'; if RegQueryStringValue(HKLM, RegPath, 'Version', InstalledVersion) then begin if CompareVersion(InstalledVersion, TargetVersion) >= 0 then begin Result := True; Exit; end; end; // 检查32位运行时(64位系统) RegPath := 'SOFTWARE\Wow6432Node\Microsoft\NET Framework Setup\NDP\v8.0\DesktopRuntime'; if RegQueryStringValue(HKLM, RegPath, 'Version', InstalledVersion) then begin if CompareVersion(InstalledVersion, TargetVersion) >= 0 then begin Result := True; Exit; end; end; end; function InitializeSetup: Boolean; var dotNetDownloaderPath: string; dotNetDownloaderArgs: string; downloadResultCode: Integer; begin Result := True; // 检测是否已安装.NET 8.0.3或更高版本的Desktop Runtime if IsNet8DesktopRuntimeInstalled('8.0.3') then begin MsgBox('.NET 8 Desktop Runtime 已安装,跳过本次安装。', mbInformation, MB_OK); Result := False; // 终止安装流程 Exit; end; dotNetDownloaderPath := ExpandConstant('{tmp}\windowsdesktop-runtime-8.0.3-win-x64.exe'); dotNetDownloaderArgs := ''; ExtractTemporaryFile('windowsdesktop-runtime-8.0.3-win-x64.exe'); // 运行下载器获取.NET 8运行时安装包 Exec(dotNetDownloaderPath, dotNetDownloaderArgs, '', SW_HIDE, ewWaitUntilTerminated, downloadResultCode); end;
代码说明
CompareVersion函数:对比两个版本字符串的大小,返回1表示前者版本更高,-1表示后者版本更高,0表示版本相等。IsNet8DesktopRuntimeInstalled函数:依次检查64位和32位运行时的注册表路径,读取已安装版本并与目标版本对比,返回是否满足安装跳过条件。InitializeSetup函数:先执行检测逻辑,若已安装符合要求的运行时则弹出提示并终止安装;否则继续执行原有的安装流程。
内容的提问来源于stack exchange,提问作者demonplus
相关产品推荐
相关产品推荐

