0

I am trying to install an application that requires .NET 4.5. I need to check if .NET 4.5 is installed or not. I've created a BAT file to check the .NET version and installer 4.5 (if not already installed) before installing my application.

I'd love to find a BAT script that I can use to check the .NET version, but everything I've found requires another tool that I can't guarantee will be on the installation computer. Instead, I wrote a small application that checks the .NET version. The application works fine BUT, I don't know how to get the program to run on all computers. If I target the application to .NET 2.0, then I run into trouble with computers that have .NET 4.5 but don't have 2.0. If I target it to run on 4.5, then obviously it requires 4.5 and can only run on computers that already have 4.5.

What is the best way to check if I need to install 4.5, that will work on any configuration of Windows?

Donn Hardy
  • 21
  • 5
  • 1
    On Windows 10, the answer is "you don't have to". Windows 10 actually came out with 4.6 already installed. 1803 includes 4.7.2. Windows 8 and later had 4.5. Windows 7 is no longer supported. Even so, installers like those created by the [Installer Projects](https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.MicrosoftVisualStudio2017InstallerProjects) or tools like NSIS or InstallShiled *already* contain actions that check for specific OS, runtime versions – Panagiotis Kanavos May 10 '18 at 15:59
  • 1
    You do not have to check, just let it [take care of it by itself](https://stackoverflow.com/a/10033128/17034). – Hans Passant May 10 '18 at 16:22

1 Answers1

0

As informed in the comments, there really should be no need to know in advance of running an install, if the installer is anyway half decent, it should stop/inform if the requirements are not met.

Here's a batch file, (Vista+), which should search the registry and list the installed versions nonetheless:

@Echo Off
SetLocal EnableDelayedExpansion
Set "rk=HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP"
Set/A "vi=ri=0"

For /F "Tokens=3*" %%A In ('Reg Query "%rk%" /F v /K') Do If "%%B"=="" (
    If Not "%%~xA"=="" (Set/A "vi+=1"
        For /F "Tokens=2*" %%C In (
            'Reg Query "%rk%\%%~nxA" /V Version 2^>Nul^|Find /V "\"'
        ) Do Set "_v!vi!=%%D"
    ) Else (Set/A "ri+=1"
        For /F "Tokens=2*" %%E In (
            'Reg Query "%rk%\%%~nxA\Full" /V Release 2^>Nul^|Find /V "\"'
        ) Do Set/A "_r!ri!=%%F"))
If %ri% Gtr 0 (Set/A "vi+=1"
    For /F "Tokens=2" %%A In ('FindStr/B "!_r%ri%!" "%~f0"'
    ) Do Set "_v%vi%=%%A") 

If %vi% Gtr 0 For /F "Tokens=1* Delims==" %%A In ('Set _v') Do Echo([%%B]

Timeout -1
GoTo :EOF

Rem Later Version Table - do not remove
378389 4.5
378675 4.5.1
378758 4.5.1
379893 4.5.2
393295 4.6
393297 4.6
394254 4.6.1
394271 4.6.1
394802 4.6.2
394806 4.6.2
460798 4.7
460805 4.7
461310 4.7.1
461310 4.7.1
Compo
  • 36,585
  • 5
  • 27
  • 39