Click here to Skip to main content
Click here to Skip to main content

How To Check if Computer has .NET Installed

, 24 Nov 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to check whether .NET is installed in PC Visual Basic 6 code

Introduction

In this article, you'll see that before you start using your .NET applications on other computers, you'll need to know whether that computer has the same .NET Framework version as your application requests to have. I searched the internet and found pieces of this problem, then completed it.

Background

The pieces I found on the net show where to look to check for the .NET version. At this website, it shows to specific Registry locations that are created after a successful .NET Framework installation. And the rest was easy. I looked for a certain module file that reaches through registry and controls it. After that, I worked a little to perfect the app and bingo... Here's how to work it.

Using the Code

After creating a VB6 project, you got to add a module file (for reaching and controlling regkeys) and paste the code below. I didn't write this code, but simply Googled it. Here's the module code:

' Registry value type definitions
Public Const REG_NONE As Long = 0
Public Const REG_SZ As Long = 1
Public Const REG_EXPAND_SZ As Long = 2
Public Const REG_BINARY As Long = 3
Public Const REG_DWORD As Long = 4
Public Const REG_LINK As Long = 6
Public Const REG_MULTI_SZ As Long = 7
Public Const REG_RESOURCE_LIST As Long = 8

' Registry section definitions
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006

' Codes returned by Reg API calls
Private Const ERROR_NONE = 0
Private Const ERROR_BADDB = 1
Private Const ERROR_BADKEY = 2
Private Const ERROR_CANTOPEN = 3
Private Const ERROR_CANTREAD = 4
Private Const ERROR_CANTWRITE = 5
Private Const ERROR_OUTOFMEMORY = 6
Private Const ERROR_INVALID_PARAMETER = 7
Private Const ERROR_ACCESS_DENIED = 8
Private Const ERROR_INVALID_PARAMETERS = 87
Private Const ERROR_NO_MORE_ITEMS = 259

' Registry API functions used in this module (there are more of them)
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (
    ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (
    ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long,
    ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (
    ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long,
    lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (
    ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String,
    lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long,
    ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (
    ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (
    ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, 
    ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (
    ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, 
    ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (
    ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String,
    ByVal cbName As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (
    ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (
    ByVal hKey As Long, ByVal lpValueName As String) As Long

' This routine allows you to get values from anywhere in the Registry, it currently
' only handles string, double word and binary values. Binary values are returned as
' hex strings.
'
' Example
' Text1.Text = ReadRegistry(HKEY_LOCAL_MACHINE, 
'   "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName")
'
Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, 
    ByVal Key As String) As String
Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long,
    sValue As String, td As Double
On Error Resume Next
lResult = RegOpenKey(Group, Section, lKeyValue)
sValue = Space$(2048)
lValueLength = Len(sValue)
lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)
If (lResult = 0) And (Err.Number = 0) Then
   If lDataTypeValue = REG_DWORD Then
      td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + 
          &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
      sValue = Format$(td, "000")
   End If
   If lDataTypeValue = REG_BINARY Then
       ' Return a binary field as a hex string (2 chars per byte)
       TStr2 = ""
       For i = 1 To lValueLength
          TStr1 = Hex(Asc(Mid(sValue, i, 1)))
          If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
          TStr2 = TStr2 + TStr1
       Next
       sValue = TStr2
   Else
      sValue = Left$(sValue, lValueLength - 1)
   End If
Else
   sValue = "Not Found"
End If
lResult = RegCloseKey(lKeyValue)
ReadRegistry = sValue
End Function		 

After that, you return to your project's main form and use this module's working function.

If you want to check for .NET v2.0 installation, use this function's return value.

ReadRegistry(HKEY_LOCAL_MACHINE, 
    "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727\", "Install") 

If you want to check for .NET v3.0 installation, use this function's return value.

ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\", 
    "Install")

If you want to check for .NET v3.5 installation, use this function's return value.

ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\", 
    "Install") 

If you want to check for .NET v4.0 installation, use this function's return value.

ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client\", 
    "Install") 

Four of the codes for checking the versions of .NET return string values either "001" or "Not Installed". If .NET exists, it returns "001" value, if not, you know the rest... Have fun...

History

  • 24th November, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kambaa

Turkey Turkey
No Biography provided

Comments and Discussions

 
GeneralOther checks PinmemberTallolo29-Nov-10 4:19 
GeneralThis ought to be done within your application installer... PinmemberFrançois Gasnier26-Nov-10 3:22 
Generalthere is no need to do sth Pinmemberjiftle24-Nov-10 15:49 
GeneralPURPOSE OF USING THIS APP PinmemberKambaa25-Nov-10 0:17 
GeneralRe: PURPOSE OF USING THIS APP Pinmemberxoox29-Nov-10 10:31 
GeneralThoughts PinmvpPIEBALDconsult24-Nov-10 9:44 
GeneralRe: Thoughts PinmemberKambaa24-Nov-10 10:03 
GeneralRe: Thoughts PinmvpLuc Pattyn24-Nov-10 13:51 
GeneralRe: Thoughts PinmvpPIEBALDconsult24-Nov-10 14:55 
GeneralRe: Thoughts PinmvpLuc Pattyn24-Nov-10 15:12 
GeneralRe: Thoughts PinmvpPIEBALDconsult24-Nov-10 17:02 
GeneralMy vote of 5 PinmemberWooters24-Nov-10 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 24 Nov 2010
Article Copyright 2010 by Kambaa
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid