Istalri Skolir
Istalri Skolir

Reputation: 124

Check if .NET 5.0 is installed in Inno Setup

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...

Upvotes: 6

Views: 2549

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202282

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 // ...

Version strings for later .NET releases have the same syntax, for example:

Microsoft.NETCore.App 6.0.28
Microsoft.NETCore.App 8.0.3

Note that the above works for .NET. For legacy .NET Framework (versions before 5.0), which uses a different registration system, use IsDotNetInstalled support function.

There's also whole library that can do the job for you:
Detecting .NET8 with Inno Setup and InnoDependencyInstaller

Upvotes: 6

Related Questions