InnoSetup撰寫偵測net8Runtime是否安裝/下載與安裝的安裝程序(InnoSetupUnicode_net8Runtime)

InnoSetup撰寫偵測net8Runtime是否安裝/下載與安裝的安裝程序(InnoSetupUnicode_net8Runtime)

InnoSetup撰寫偵測net8Runtime是否安裝/下載與安裝的安裝程序(InnoSetupUnicode_net8Runtime)


資料來源: copilot


GITHUB: https://github.com/jash-git/InnoSetup_net8Runtime_script


AI 問題:

Inno Setup Script 實作偵測系統是否安裝.net8 WindowsDesktop Runtime 8.0.12 

以下是REGEDIT 當有安裝確切的紀錄路徑 HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.WindowsDesktop.App 

將wget.exe 打包進安裝檔中 一起發布

如果沒有安裝 就立刻使用在安裝目錄下的curl.exe直接下載
https://download.visualstudio.microsoft.com/download/pr/f1e7ffc8-c278-4339-b460-517420724524/f36bb75b2e86a52338c4d3a90f8dac9b/windowsdesktop-runtime-8.0.12-win-x64.exe

並且執行安裝 的 完整可以編譯範例


code:00_ne8_Runtime_檢查NET8_OK.iss

; 定義一些常量
#define RuntimeVersion "8.0.12"
#define RuntimeURL "https://download.visualstudio.microsoft.com/download/pr/f1e7ffc8-c278-4339-b460-517420724524/f36bb75b2e86a52338c4d3a90f8dac9b/windowsdesktop-runtime-8.0.12-win-x64.exe"

[Setup]
AppName=YourAppName
AppVersion=1.0
DefaultDirName={pf}\YourAppName
DefaultGroupName=YourAppName
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Files]
; 將 wget.exe 打包進安裝檔中
Source: "wget.exe"; DestDir: "{tmp}"; Flags: ignoreversion

[Code]
const
  RuntimeKeyBase1 = 'SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.WindowsDesktop.App';
  RuntimeKeyBase2 = 'SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.WindowsDesktop.App';
  RuntimeVersionKey = '{#RuntimeVersion}';

function IsRuntimeInstalled: Boolean;
var
  InstallKey1, InstallKey2: String;
  InstallLocation, Version, DebugMsg: String;
begin
  Result := False;
  InstallKey1 := RuntimeKeyBase1 + '\' + RuntimeVersionKey;
  InstallKey2 := RuntimeKeyBase2 + '\' + RuntimeVersionKey;
  MsgBox('正在檢查註冊表鍵:' + InstallKey1, mbInformation, MB_OK);

  if RegQueryStringValue(HKLM, InstallKey1, 'InstallLocation', InstallLocation) then
  begin
    DebugMsg := '找到安裝位置:' + InstallLocation;
    if InstallLocation <> '' then
    begin
      Result := True;
    end
    else
    begin
      DebugMsg := DebugMsg + ' (InstallLocation 為空)';
    end;
  end
  else if RegQueryStringValue(HKLM, InstallKey1, 'Version', Version) then
  begin
    DebugMsg := '找到版本:' + Version;
    if Version <> '' then
    begin
      Result := True;
    end
    else
    begin
      DebugMsg := DebugMsg + ' (Version 為空)';
    end;
  end
  else
  begin
    DebugMsg := '未找到 InstallLocation 或 Version 值在 ' + InstallKey1 + '。';
  end;

  if not Result then
  begin
    MsgBox('正在檢查註冊表鍵:' + InstallKey2, mbInformation, MB_OK);

    if RegQueryStringValue(HKLM, InstallKey2, 'InstallLocation', InstallLocation) then
    begin
      DebugMsg := '找到安裝位置:' + InstallLocation;
      if InstallLocation <> '' then
      begin
        Result := True;
      end
      else
      begin
        DebugMsg := DebugMsg + ' (InstallLocation 為空)';
      end;
    end
    else if RegQueryStringValue(HKLM, InstallKey2, 'Version', Version) then
    begin
      DebugMsg := '找到版本:' + Version;
      if Version <> '' then
      begin
        Result := True;
        MsgBox('.NET 8 WindowsDesktop Runtime 已安裝,版本:' + Version, mbInformation, MB_OK);
      end
      else
      begin
        DebugMsg := DebugMsg + ' (Version 為空)';
      end;
    end
    else
    begin
      DebugMsg := DebugMsg + ' 未找到 InstallLocation 或 Version 值在 ' + InstallKey2 + '。';
    end;
  end;

  MsgBox(DebugMsg, mbInformation, MB_OK);

  if Result then
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 未安裝。', mbInformation, MB_OK);
  end;
end;

procedure DownloadAndInstallRuntime();
var
  ResultCode: Integer;
  URL, OutputFile, TempPath: String;
begin
  MsgBox('即將開始下載 .NET 8 WindowsDesktop Runtime。', mbInformation, MB_OK);
  URL := '{#RuntimeURL}';
  TempPath := ExpandConstant('{tmp}');
  OutputFile := TempPath + '\windowsdesktop-runtime-{#RuntimeVersion}-win-x64.exe';
  if not FileExists(OutputFile) then
  begin
    Exec(ExpandConstant('{tmp}\wget.exe'), URL + ' -O ' + OutputFile, '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
  end;
  if FileExists(OutputFile) then
  begin
    Exec(OutputFile, '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
  end;
end;

function InitializeSetup(): Boolean;
begin
  Result := True;
  if IsRuntimeInstalled() then
  begin
    MsgBox('已偵測到 .NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    DownloadAndInstallRuntime();
  end;
end;


code:01_ne8_Runtime_內建CURL下載與安裝_OK.iss

; 定義一些常量
#define RuntimeVersion "8.0.12"
#define RuntimeURL "https://download.visualstudio.microsoft.com/download/pr/f1e7ffc8-c278-4339-b460-517420724524/f36bb75b2e86a52338c4d3a90f8dac9b/windowsdesktop-runtime-8.0.12-win-x64.exe"

[Setup]
AppName=YourAppName
AppVersion=1.0
DefaultDirName={pf}\YourAppName
DefaultGroupName=YourAppName
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Files]
; 將 wget.exe 打包進安裝檔中
Source: "wget.exe"; DestDir: "{tmp}"; Flags: ignoreversion

[Code]
const
  RuntimeKey = 'SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.WindowsDesktop.App';
  RuntimeVersionKey = '{#RuntimeVersion}';

function IsRuntimeInstalled: Boolean;
var
  InstallKey: String;
  Value: Cardinal;
  DebugMsg: String;
begin
  Result := False;
  InstallKey := RuntimeKey + '\' + RuntimeVersionKey;
  MsgBox('正在檢查註冊表鍵:' + InstallKey, mbInformation, MB_OK);

  if RegQueryDWordValue(HKLM, RuntimeKey, RuntimeVersionKey, Value) then
  begin
    DebugMsg := '找到 DWORD 值:' + IntToStr(Value);
    if Value = 1 then
    begin
      Result := True;
      DebugMsg := DebugMsg + ' (值匹配)';
    end
    else
    begin
      DebugMsg := DebugMsg + ' (值不匹配)';
    end;
  end
  else
  begin
    DebugMsg := '未找到 DWORD 值。';
  end;

  MsgBox(DebugMsg, mbInformation, MB_OK);

  if Result then
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 未安裝。', mbInformation, MB_OK);
  end;
end;

procedure DownloadAndInstallRuntime();
var
  ResultCode: Integer;
  URL, OutputFile, TempPath: String;
  DownloadSuccess: Boolean;
begin
  MsgBox('即將開始下載 .NET 8 WindowsDesktop Runtime。', mbInformation, MB_OK);
  URL := '{#RuntimeURL}';
  TempPath := ExpandConstant('{tmp}');
  OutputFile := TempPath + '\windowsdesktop-runtime-{#RuntimeVersion}-win-x64.exe';
  DownloadSuccess := False;

  if not FileExists(OutputFile) then
  begin
    Exec(ExpandConstant('{tmp}\wget.exe'), '--no-check-certificate ' + URL + ' -O ' + OutputFile, '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
    if ResultCode = 0 then
    begin
      DownloadSuccess := True;
      MsgBox('.NET 8 WindowsDesktop Runtime 下載成功。', mbInformation, MB_OK);
    end
    else
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 下載失敗。錯誤碼:' + IntToStr(ResultCode), mbError, MB_OK);
    end;
  end
  else
  begin
    DownloadSuccess := True;
    MsgBox('.NET 8 WindowsDesktop Runtime 文件已存在,無需下載。', mbInformation, MB_OK);
  end;

  if DownloadSuccess then
  begin
    Exec(OutputFile, '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
    if ResultCode = 0 then
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 安裝成功。', mbInformation, MB_OK);
    end
    else
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 安裝失敗。錯誤碼:' + IntToStr(ResultCode), mbError, MB_OK);
    end;
  end;
end;

function InitializeSetup(): Boolean;
begin
  Result := True;
  if IsRuntimeInstalled() then
  begin
    MsgBox('已偵測到 .NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    DownloadAndInstallRuntime();
  end;
end;


code:02_ne8_Runtime_外掛CURL下載與安裝_OK.iss

; 定義一些常量
#define RuntimeVersion "8.0.12"
#define RuntimeURL "https://download.visualstudio.microsoft.com/download/pr/f1e7ffc8-c278-4339-b460-517420724524/f36bb75b2e86a52338c4d3a90f8dac9b/windowsdesktop-runtime-8.0.12-win-x64.exe"

[Setup]
AppName=YourAppName
AppVersion=1.0
DefaultDirName={pf}\YourAppName
DefaultGroupName=YourAppName
OutputBaseFilename=setup
Compression=lzma
SolidCompression=yes

[Files]
; 將 curl.exe 和 curl-ca-bundle.crt 打包進安裝檔中
Source: "curl.exe"; DestDir: "{tmp}"; Flags: ignoreversion
Source: "curl-ca-bundle.crt"; DestDir: "{tmp}"; Flags: ignoreversion

[Code]
const
  RuntimeKey = 'SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x64\sharedfx\Microsoft.WindowsDesktop.App';
  RuntimeVersionKey = '{#RuntimeVersion}';

function IsRuntimeInstalled: Boolean;
var
  InstallKey: String;
  Value: Cardinal;
  DebugMsg: String;
begin
  Result := False;
  InstallKey := RuntimeKey + '\' + RuntimeVersionKey;
  MsgBox('正在檢查註冊表鍵:' + InstallKey, mbInformation, MB_OK);

  if RegQueryDWordValue(HKLM, RuntimeKey, RuntimeVersionKey, Value) then
  begin
    DebugMsg := '找到 DWORD 值:' + IntToStr(Value);
    if Value = 1 then
    begin
      Result := True;
      DebugMsg := DebugMsg + ' (值匹配)';
    end
    else
    begin
      DebugMsg := DebugMsg + ' (值不匹配)';
    end;
  end
  else
  begin
    DebugMsg := '未找到 DWORD 值。';
  end;

  MsgBox(DebugMsg, mbInformation, MB_OK);

  if Result then
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    MsgBox('.NET 8 WindowsDesktop Runtime 未安裝。', mbInformation, MB_OK);
  end;
end;

function CopyCurlFilesToTemp(): Boolean;
begin
  Result := False;
  if FileExists(ExpandConstant('{tmp}\curl.exe')) and FileExists(ExpandConstant('{tmp}\curl-ca-bundle.crt')) then
  begin
    MsgBox('curl.exe 和 curl-ca-bundle.crt 已成功複製到 {tmp} 資料夾。', mbInformation, MB_OK);
    Result := True;
  end
  else
  begin
    MsgBox('curl.exe 或 curl-ca-bundle.crt 未成功複製到 {tmp} 資料夾。', mbError, MB_OK);
  end;
end;

procedure ExtractCurlFiles();
begin
  ExtractTemporaryFile('curl.exe');
  ExtractTemporaryFile('curl-ca-bundle.crt');
end;

procedure DownloadAndInstallRuntime();
var
  ResultCode: Integer;
  URL, TempPath: String;
  DownloadSuccess: Boolean;
begin
  MsgBox('即將開始下載 .NET 8 WindowsDesktop Runtime。', mbInformation, MB_OK);
  URL := '{#RuntimeURL}';
  TempPath := ExpandConstant('{tmp}');
  DownloadSuccess := False;

  if CopyCurlFilesToTemp() then
  begin
    Exec(TempPath + '\curl.exe', '-L -O ' + URL + ' --cacert ' + TempPath + '\curl-ca-bundle.crt', TempPath, SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
    if ResultCode = 0 then
    begin
      DownloadSuccess := True;
      MsgBox('.NET 8 WindowsDesktop Runtime 下載成功。', mbInformation, MB_OK);
    end
    else
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 下載失敗。錯誤碼:' + IntToStr(ResultCode), mbError, MB_OK);
    end;
  end
  else
  begin
    Exit; // 終止安裝過程
  end;

  if DownloadSuccess then
  begin
    Exec(TempPath + '\windowsdesktop-runtime-{#RuntimeVersion}-win-x64.exe', '', '', SW_SHOWNORMAL, ewWaitUntilTerminated, ResultCode);
    if ResultCode = 0 then
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 安裝成功。', mbInformation, MB_OK);
    end
    else
    begin
      MsgBox('.NET 8 WindowsDesktop Runtime 安裝失敗。錯誤碼:' + IntToStr(ResultCode), mbError, MB_OK);
    end;
  end;
end;

function InitializeSetup(): Boolean;
begin
  Result := True;
  
  ExtractCurlFiles();
  
  if not CopyCurlFilesToTemp() then
  begin
    MsgBox('無法複製 curl 文件到 {tmp} 資料夾。', mbError, MB_OK);
    Result := False;
    Exit;
  end;

  if IsRuntimeInstalled() then
  begin
    MsgBox('已偵測到 .NET 8 WindowsDesktop Runtime 已安裝。', mbInformation, MB_OK);
  end
  else
  begin
    DownloadAndInstallRuntime();
  end;
end;

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *