Does anyone know how to check for Version 2 of the .NET Framework during Runtime? VB.NET

Does anyone know how to check for Version 2 of the .NET Framework during Runtime? VB.NET
Who is Participating?
Corey ScheichDeveloperCommented:
 I have used this to figure out what the current version is I couldn't find a regKey so I used the install folders to figure it out.

Public Function GetCurrentFrameworkVersionString () as string    

        Dim hklm As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine
        Dim hkcu As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser
        Dim Key As Microsoft.Win32.RegistryKey = hklm.OpenSubKey("SOFTWARE\Microsoft\.NETFramework\")
        Dim FrameWork As System.IO.DirectoryInfo
        Dim CurrentFrameWork() As System.IO.DirectoryInfo

        If Not Key Is Nothing Then
            Dim count As Long = 0
            Dim RegAsmPath As String = ""
            Dim Version As String = ""

            FrameWork = New System.IO.DirectoryInfo(Key.GetValue("InstallRoot", "C:\WINDOWS\Microsoft.NET\Framework\"))
            CurrentFrameWork = FrameWork.GetDirectories("v*.*")
            For count = 0 To CurrentFrameWork.GetUpperBound(0)
                If CurrentFrameWork(count).Name > Version Then
                    Version = CurrentFrameWork(count).Name
                    RegAsmPath = CurrentFrameWork(count).FullName & "\Regasm.exe"
                End If
            Return Version
End Function
Carl TawnSystems and Integration DeveloperCommented:
If your app is compiled against .Net 2.0 and you try to run it on a machine without .Net 2.0 then it will fail. You wouldn't be able to check for it because your app won't run. Bit of a "Catch 22" situation.
pgilfeatherAuthor Commented:
Is there any situation that this might not work?

Could the framework have been installed in a slightly different directory depending on the operating system?
Corey ScheichDeveloperCommented:
I think it should work on any Windows Machine I don't know about Mac Linux and such.
if the registry key for the InstallRoot (which should contain the .Net installation location) isn't found then it defaults to "C:\WINDOWS\Microsoft.NET\Framework\" which is the installation location for a single boot machine that has windows XP installed.

Alternatively you could do this.

       Dim Key As Microsoft.Win32.RegistryKey = hklm.OpenSubKey("SOFTWARE\Microsoft\.NETFramework\policy\v2.0")

       if Key.GetValue("50727", "") <> "50727-50727" then
             msgbox "The v2.0 of the .NET Framework was not found on this system"
             msgbox "The v2.0 of the .NET Framework was found on this system"
      end if

this is the key that is checked in an install program as described in this article.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.