4

I have the following .iss script to compile a games launcher I'm working on that uses .NET 5.0. Currently it tries to install .NET 5.0 from the installer it has every time instead of checking if its required first. I've found plenty of resources that tell you how to do it for the .NET Framework but hardly anything for .NET 5.0 which is a updated version of .NET Core. How do I check if .NET 5.0 is already installed before trying to install it anyway?

I also am aware that 5.0 is nearer end of life but I'm using Visual Studio 2019 which 6.0 isn't compatible with and would prefer not to have to use any work arounds to get 2019 to play ball with it.

#define AppName "LowPoly Games Launcher"
#define AppEXEName "LPG Launcher.exe"

[Setup]
AppName={#AppName}

[Files]
Source: "..\bin\Release\net5.0-windows\*"; DestDir: "{app}"; \
    Flags: ignoreversion recursesubdirs;
Source: "Resources\windowsdesktop-runtime-5.0.17-win-x64.exe"; \
    DestDir: "{app}"; Flags: ignoreversion deleteafterinstall

[Run]
Filename: "{app}\{#AppEXEName}"; \
    Description: "{cm:LaunchProgram, {#StringChange(AppName, '&', '&&')}}"; \
    Flags: nowait postinstall
Filename: "{app}\windowsdesktop-runtime-5.0.17-win-x64.exe"; \
    Parameters: "/q/passive"; Flags: waituntilterminated; \
    StatusMsg: Microsoft .NET Framework 5.0 is being installed. Please wait...
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Istalri Skolir
  • 96
  • 1
  • 11

1 Answers1

4

Based on:

You can do:

[Code]

function IsDotNetInstalled(DotNetName: string): Boolean;
var
  Cmd, Args: string;
  FileName: string;
  Output: AnsiString;
  Command: string;
  ResultCode: Integer;
begin
  FileName := ExpandConstant('{tmp}\dotnet.txt');
  Cmd := ExpandConstant('{cmd}');
  Command := 'dotnet --list-runtimes';
  Args := '/C ' + Command + ' > "' + FileName + '" 2>&1';
  if Exec(Cmd, Args, '', SW_HIDE, ewWaitUntilTerminated, ResultCode) and
     (ResultCode = 0) then
  begin
    if LoadStringFromFile(FileName, Output) then
    begin
      if Pos(DotNetName, Output) > 0 then
      begin
        Log('"' + DotNetName + '" found in output of "' + Command + '"');
        Result := True;
      end
        else
      begin
        Log('"' + DotNetName + '" not found in output of "' + Command + '"');
        Result := False;
      end;
    end
      else
    begin
      Log('Failed to read output of "' + Command + '"');
    end;
  end
    else
  begin
    Log('Failed to execute "' + Command + '"');
    Result := False;
  end;
  DeleteFile(FileName);
end;

And it use it like:

if IsDotNetInstalled('Microsoft.NETCore.App 5.0.') then // ...
Siarhei Kuchuk
  • 5,296
  • 1
  • 28
  • 31
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thank you! This was just the kind of solution I was looking for! I had found another but it was unreliable so will give this one a shot. – Istalri Skolir Jun 07 '22 at 15:11