0

I'm currently trying to find out if .NET 4.0 is installed on different PC via command-line. The command I'm using is:

REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Version

If 4.0 is installed I get 4.0..... as a result while if nothing is installed at all I get a registry key error.

Now my question is (as I can't reproduce it here):

Is that secure enough, or could I run into troubles in case 2.0 is installed or 3.0 but NOT 4.0? Thus would I still get the registry key not found error or would I have to use something different here?

Paul
  • 2,620
  • 2
  • 17
  • 27
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • 3
    See the official article [How to: Determine Which .NET Framework Versions Are Installed](https://msdn.microsoft.com/en-us/library/hh925568(v=vs.110).aspx). I think it covers all of the question. – wOxxOm Oct 01 '15 at 10:47
  • Tnx but I saw that one already. Problem is that it uses c#/.net mostly to get the infos but in my case I need to do it without .NET involved thus I used reg query. Looking for the same stats in essence but I'm not sure what happens if it is 2.0 or 3.0 installed instead of 4.0. If the keys are not there then (resulting in a key not found error which would be the best possible outcome, or if there is some other result there). With no version installed its the key not found error, but with 2.0 or 3.0 I'm not sure and the article doesn't cover that sadly. – Thomas Oct 01 '15 at 11:03
  • 1
    Microsoft got pretty fed up with having to deal with customer service calls, the kind where they are looking for a workaround for a broken version check in software. So they made it intentionally hard to check versions. The Windows version number is notable, they intentionally lie about it and return a lower number. What they *want* you to do is simply run the installer. It will be done *very* quickly when the proper revision with the latest updates and security fixes is already present. – Hans Passant Oct 01 '15 at 12:35
  • Only that in cases where developers need to make sure that specific versions are there or not.........they get more calls that way (at least if stackoverflow wouldn't exist that would be the case) – Thomas Oct 01 '15 at 12:37
  • 1
    Out of curiosity, what are you doing that you need to check the installed .NET version outside of a installer? – Scott Chamberlain Oct 01 '15 at 13:53
  • A log parser that needs to run on non pc machines which have windows installed. The distribution onto them has to be without an installer (which is why I need(ed) to check preliminarily if they have the correct version of the framework or not.......if they did not my program wouldn't even be able to be called (found that out when I accidently used framework 4.5 instead of 4.0). And as I have no permission to install the .NET framework if missing I had to check the hard way (aka the above batch) but needed to know if that is enough of a check or if something additional needed to be done. – Thomas Oct 01 '15 at 16:25

1 Answers1

0

To check if v4 is installed is quite simple.

dir /b /ad "%windir%\microsoft.net\Framework\v4.*"

return

v4.0.30319

If you want to be sure to get the latest version installed:

@echo off

for /f "delims=" %%a in (
   'dir /b /ad "%windir%\microsoft.net\Framework\*" ^|sort /r ^|findstr /r [0-9]'
) do (
      set "version=%%a"
      goto done
)
exit /b 0

:done
echo version %version%

If you need to run a latest specific executable for example csc.exe

@echo off

for /f "delims=" %%a in (
   'dir /b /s /a-d "%windir%\microsoft.net\Framework\*csc.exe"^|sort /r'
) do (
      set "executable=%%a"
      goto done
)
exit /b 0

:done
echo executable %executable%

I just found here other ways to verify .NET is installed with wmic but is quite slow on my setup.

Community
  • 1
  • 1
Paul
  • 2,620
  • 2
  • 17
  • 27