12

Is there a way to check if the .NET Framework 4 has been installed and install it only when it's not in the system?

I know, how do I determine, if the .NET Framework 4 is installed by checking the following registry key?

hasDotnet4 :=
  RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\.NETFramework\policy\v4.0');

How do I conditionally run the .NET Framework 4 installation based on the above check?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Phillip Macdonald
  • 434
  • 1
  • 4
  • 16
  • 1
    possible duplicate of [Im using inno setup to detect if .net 4.0 client installed but it doesn't work well](http://stackoverflow.com/questions/9008905/im-using-inno-setup-to-detect-if-net-4-0-client-installed-but-it-doesnt-work-w) – Bernard Apr 11 '12 at 17:41
  • No, this is unique, just couldn't find any threads with this answer – Phillip Macdonald Apr 11 '12 at 20:23

1 Answers1

28

The easiest you can do, is to use the Check parameter, which allows you to control if a certain file from the [Files] section will be extracted, or if a certain program from the [Run] section will be executed. The following script code shows its usage for the conditional installation of the .NET Framework 4:

[Files]
Source: "dotNetFx40_Full_setup.exe"; DestDir: {tmp}; \
  Flags: deleteafterinstall; Check: FrameworkIsNotInstalled

[Run]
Filename: "{tmp}\dotNetFx40_Full_setup.exe"; Check: FrameworkIsNotInstalled

[Code]

function FrameworkIsNotInstalled: Boolean;
begin
  Result :=
    not RegKeyExists(
      HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\.NETFramework\policy\v4.0');
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 4
    You could have the `Check:` statement even in the `[Files]` section directly to skip even the extraction step. – Gogowitsch Jul 09 '12 at 07:32
  • Why not with `ExpandConstant('{dotnet40}')` instead of direct registry access? – i486 Apr 16 '15 at 12:05
  • @i486, checking the registry key is better because you're not adding the exception handling overhead. Except that you can easily modify this code to detect a specific version, e.g. .NET 4.5. – TLama Apr 16 '15 at 12:20