Click here to Skip to main content
15,881,852 members
Articles / Desktop Programming / Windows Forms

How To Check if Computer has .NET Installed

Rate me:
Please Sign up or sign in to vote.
4.57/5 (7 votes)
24 Nov 2010CPOL1 min read 49.8K   1.3K   33   12
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:

VB.NET
' 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.

VB.NET
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.

VB.NET
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.

VB.NET
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.

VB.NET
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)


Written By
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralOther checks Pin
Tallmaris29-Nov-10 3:19
Tallmaris29-Nov-10 3:19 
GeneralThis ought to be done within your application installer... Pin
François Gasnier26-Nov-10 2:22
François Gasnier26-Nov-10 2:22 
Generalthere is no need to do sth Pin
jiftle24-Nov-10 14:49
jiftle24-Nov-10 14:49 
GeneralPURPOSE OF USING THIS APP Pin
Kambaa24-Nov-10 23:17
Kambaa24-Nov-10 23:17 
i was trying to create a autorun file for my dvd and i used c# code...i will use this dvd for many pc's and i will not know whether it has dotnet installed or not. so i created a pre-run vb6 app that first checks for dotnet installation and then runs main app. if not, it asks you to install it. this posted app is simply the base line of this purpose... i know that if somebdy can alter manually changes the regkeys.... and cannot be used for other env's... Just sayin'...
Cheers...
GeneralRe: PURPOSE OF USING THIS APP Pin
xoox29-Nov-10 9:31
xoox29-Nov-10 9:31 
GeneralThoughts Pin
PIEBALDconsult24-Nov-10 8:44
mvePIEBALDconsult24-Nov-10 8:44 
GeneralRe: Thoughts Pin
Kambaa24-Nov-10 9:03
Kambaa24-Nov-10 9:03 
GeneralRe: Thoughts Pin
Luc Pattyn24-Nov-10 12:51
sitebuilderLuc Pattyn24-Nov-10 12:51 
GeneralRe: Thoughts Pin
PIEBALDconsult24-Nov-10 13:55
mvePIEBALDconsult24-Nov-10 13:55 
GeneralRe: Thoughts Pin
Luc Pattyn24-Nov-10 14:12
sitebuilderLuc Pattyn24-Nov-10 14:12 
GeneralRe: Thoughts Pin
PIEBALDconsult24-Nov-10 16:02
mvePIEBALDconsult24-Nov-10 16:02 
GeneralMy vote of 5 Pin
Wooters24-Nov-10 5:42
Wooters24-Nov-10 5:42 

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

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