Retrieve current installed .NET version

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
wriedmann
Posts: 3649
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Retrieve current installed .NET version

Post by wriedmann »

Hello,

to retrieve the current version of the .NET Framework, you can use this code:

Code: Select all

function GetLatestVersion4() as string
	local cReturn as string
	local oMainKey as RegistryKey
	local oKey as RegistryKey
	local nClientRelease as int
	local nFullRelease as int
	local cClientVersion as string
	local cFullVersion as string
	local nRelease as int
	local cVersion as string
	
	cReturn := ""
	
	oMainKey	 := RegistryKey.OpenBaseKey( RegistryHive.LocalMachine, RegistryView.Default )
	oKey := oMainKey:OpenSubKey( "SOFTWAREMicrosoftNET Framework SetupNDPv4Client" ) 
	nClientRelease := ( int ) oKey:GetValue( "Release", 0 )
	cClientVersion := ( string ) oKey:GetValue( "Version", "" )
	oKey:Dispose()
	oKey := null
	
	oKey := oMainKey:OpenSubKey( "SOFTWAREMicrosoftNET Framework SetupNDPv4Full" ) 
	nFullRelease := ( int ) oKey:GetValue( "Release", 0 )
	cFullVersion := ( string ) oKey:GetValue( "Version", "" )
	oKey:Dispose()
	oKey := null
	oMainKey:Dispose()
	oMainKey := null
	
	if nFullRelease > nClientRelease
		nRelease := nFullRelease
	else	
		nRelease := nClientRelease
	endif
	if cFullVersion > cClientVersion
		cVersion := cFullVersion
	else	
		cVersion := cClientVersion
	endif
	
	do case
	case nRelease == 0
		cReturn := cVersion
	case nRelease == 378389
		cReturn := ".NET Framework 4.5"
	case nRelease == 378675
		cReturn := ".NET Framework 4.5.1 - Windows 8.1"
	case nRelease == 378758
		cReturn := ".NET Framework 4.5.1 - Windows 8, Windows 7 SP1, Windows Vista SP2"
	case nRelease == 379893
		cReturn := ".NET Framework 4.5.2"
	case nRelease == 393295
		cReturn := ".NET Framework 4.6 - Windows 10"
	case nRelease == 393297
		cReturn := ".NET Framework 4.6 - other"
	case nRelease == 394254
		cReturn := ".NET Framework 4.6.1 - Windows 10"
	case nRelease == 394271
		cReturn := ".NET Framework 4.6.1 - other"
	case nRelease == 394802
		cReturn := ".NET Framework 4.6.2 - Windows 10 Anniversary Update"
	case nRelease == 394806
		cReturn := ".NET Framework 4.6.2 - other"
	case nRelease == 460798
		cReturn := ".NET Framework 4.7 - Windows 10 Creators Update"
	case nRelease == 460805
		cReturn := ".NET Framework 4.7 - other"
	case nRelease == 461308
		cReturn := ".NET Framework 4.7.1 - Windows 10 Fall Creators Update"
	case nRelease == 461310
		cReturn := ".NET Framework 4.7.1 - other"
	case nRelease == 461808
		cReturn := ".NET Framework 4.7.2 - Windows 10 April 2018 Update"
	case nRelease == 461814
		cReturn := ".NET Framework 4.7.2 - other"
	otherwise
		cReturn := ".NET Framework " + nRelease:ToString()
	endcase

	return cReturn
You can find a complete application as XIDE export file with this code here:
GetDotNETVersion.zip
(16.92 KiB) Downloaded 84 times
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

Retrieve current installed .NET version

Post by Karl-Heinz »

Hi Wolfgang,

work´s fine.

win 8.1 -> ".NET Framework 4.7.2 - other"
Win10 -> ".NET Framework 4.7.2 - Windows 10 April 2018 Update"

BTW. What´s the reason why your viaef references do not point to the GAC ?

regards
Karl-Heinz
User avatar
wriedmann
Posts: 3649
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Retrieve current installed .NET version

Post by wriedmann »

Hi Karl-Heinz.

very simple: if I use the references from the GAC, this application will not run on all .NET 4 versions.

I have built it with v4.0 references, so it will run even on Windows XP (where no .NET 4.5 is available, only 4.0).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4573
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Retrieve current installed .NET version

Post by Chris »

Hi Wolfgang.

I think it should always work if you have GAC references. Have you found a specific case where this does not work?

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3649
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Retrieve current installed .NET version

Post by wriedmann »

Hi Chris,

no, when using the GAC references it does not run under Windows XP and not under Windows installations that don't have the latest .NET Framework (had issues with a customer on a Win 7 machine where for two year no update was installed).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply