如何通过Inno Setup检测并安装.NET 6 Runtime?
实现Inno Setup检测并自动安装.NET 6 Runtime(WPF程序)
1. 检测.NET 6 Runtime是否已安装
.NET 6 Runtime的安装信息会写入Windows注册表,可通过读取对应注册表项判断是否已安装:
- x64系统:检查
HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.NETCore.App下是否存在版本≥6.0.0的子键 - x86系统:检查
HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\sharedfx\Microsoft.NETCore.App下的对应版本子键
在Inno Setup的[Code]段实现检测函数:
function ParseVersion(VersionStr: String; var Major, Minor, Build: Integer): Boolean; var Parts: TArrayOfString; begin Result := False; if SplitString(VersionStr, '.', Parts) >= 2 then begin Major := StrToIntDef(Parts[0], 0); Minor := StrToIntDef(Parts[1], 0); Build := StrToIntDef(Parts[2], 0); Result := True; end; end; function IsNet6RuntimeInstalled: Boolean; var RegPath: String; KeyHandle: Integer; I: Integer; VersionStr: String; Major, Minor, Build: Integer; begin Result := False; if IsWin64 then RegPath := 'SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.NETCore.App' else RegPath := 'SOFTWARE\dotnet\Setup\InstalledVersions\x86\sharedfx\Microsoft.NETCore.App'; if RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_READ, KeyHandle) = 0 then begin I := 0; while RegEnumKeyEx(KeyHandle, I, VersionStr, nil, nil, nil, nil, nil) = 0 do begin if ParseVersion(VersionStr, Major, Minor, Build) then begin if Major >= 6 then begin Result := True; Break; end; end; I := I + 1; end; RegCloseKey(KeyHandle); end; end;
2. 打包本地.NET 6 Runtime安装包
将你下载的x86/x64版本Windows Desktop Runtime安装包(WPF依赖此版本,而非基础Core Runtime)放到脚本目录,在[Files]段添加:
[Files] ; 主程序文件 Source: "你的WPF程序.exe"; DestDir: "{app}"; Flags: ignoreversion ; 其他程序依赖文件... ; .NET 6 Runtime安装包(仅在未安装时释放) Source: "windowsdesktop-runtime-6.0.x-win-x86.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall; Check: not IsWin64 or (IsWin64 and not IsNet6RuntimeInstalled) Source: "windowsdesktop-runtime-6.0.x-win-x64.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall; Check: IsWin64 and not IsNet6RuntimeInstalled
3. 编写自动安装逻辑
在[Code]段添加初始化检测逻辑,未安装时自动启动静默安装:
procedure InitializeSetup; var InstallerPath: String; ResultCode: Integer; begin if not IsNet6RuntimeInstalled then begin MsgBox('检测到未安装.NET 6 Windows Desktop Runtime,将自动安装。', mbInformation, MB_OK); // 根据系统架构选择对应安装包 if IsWin64 then InstallerPath := ExpandConstant('{tmp}\windowsdesktop-runtime-6.0.x-win-x64.exe') else InstallerPath := ExpandConstant('{tmp}\windowsdesktop-runtime-6.0.x-win-x86.exe'); // 静默安装参数:/quiet无交互,/norestart不强制重启 if not Exec(InstallerPath, '/quiet /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin MsgBox('安装.NET 6 Runtime失败,错误代码: ' + IntToStr(ResultCode), mbError, MB_OK); Abort; // 终止主程序安装 end; end; end;
4. 完整示例脚本
整合所有部分的完整脚本模板:
[Setup] AppName=你的WPF程序名称 AppVersion=1.0.0 DefaultDirName={autopf}\你的WPF程序名称 DefaultGroupName=你的WPF程序名称 OutputDir=.\Output OutputBaseFilename=你的WPF程序安装包 Compression=lzma2/ultra SolidCompression=yes ArchitecturesInstallIn64BitMode=x64 [Files] Source: "你的WPF程序.exe"; DestDir: "{app}"; Flags: ignoreversion ; 其他程序依赖文件... Source: "windowsdesktop-runtime-6.0.x-win-x86.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall; Check: not IsWin64 or (IsWin64 and not IsNet6RuntimeInstalled) Source: "windowsdesktop-runtime-6.0.x-win-x64.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall; Check: IsWin64 and not IsNet6RuntimeInstalled [Icons] Name: "{group}\你的WPF程序"; Filename: "{app}\你的WPF程序.exe" Name: "{commondesktop}\你的WPF程序"; Filename: "{app}\你的WPF程序.exe"; Flags: commonprograms desktopicon [Code] function ParseVersion(VersionStr: String; var Major, Minor, Build: Integer): Boolean; var Parts: TArrayOfString; begin Result := False; if SplitString(VersionStr, '.', Parts) >= 2 then begin Major := StrToIntDef(Parts[0], 0); Minor := StrToIntDef(Parts[1], 0); Build := StrToIntDef(Parts[2], 0); Result := True; end; end; function IsNet6RuntimeInstalled: Boolean; var RegPath: String; KeyHandle: Integer; I: Integer; VersionStr: String; Major, Minor, Build: Integer; begin Result := False; if IsWin64 then RegPath := 'SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.NETCore.App' else RegPath := 'SOFTWARE\dotnet\Setup\InstalledVersions\x86\sharedfx\Microsoft.NETCore.App'; if RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegPath, 0, KEY_READ, KeyHandle) = 0 then begin I := 0; while RegEnumKeyEx(KeyHandle, I, VersionStr, nil, nil, nil, nil, nil) = 0 do begin if ParseVersion(VersionStr, Major, Minor, Build) then begin if Major >= 6 then begin Result := True; Break; end; end; I := I + 1; end; RegCloseKey(KeyHandle); end; end; procedure InitializeSetup; var InstallerPath: String; ResultCode: Integer; begin if not IsNet6RuntimeInstalled then begin MsgBox('检测到未安装.NET 6 Windows Desktop Runtime,将自动安装。', mbInformation, MB_OK); if IsWin64 then InstallerPath := ExpandConstant('{tmp}\windowsdesktop-runtime-6.0.x-win-x64.exe') else InstallerPath := ExpandConstant('{tmp}\windowsdesktop-runtime-6.0.x-win-x86.exe'); if not Exec(InstallerPath, '/quiet /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin MsgBox('安装.NET 6 Runtime失败,错误代码: ' + IntToStr(ResultCode), mbError, MB_OK); Abort; end; end; end;
注意事项
- 替换脚本中所有占位符(如
你的WPF程序名称、windowsdesktop-runtime-6.0.x-win-x86.exe)为实际内容 - 确保下载的是Windows Desktop Runtime版本,WPF程序依赖此运行时环境
- 若需要安装后强制重启,可移除静默参数中的
/norestart
内容的提问来源于stack exchange,提问作者Seraphine
相关产品推荐
相关产品推荐

