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

Posted on 2006-03-29
Last Modified: 2010-04-23
Does anyone know how to check for Version 2 of the .NET Framework during Runtime? VB.NET
Question by:pgilfeather
    LVL 52

    Expert Comment

    by:Carl Tawn
    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.
    LVL 13

    Accepted Solution

     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

    Author Comment

    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?
    LVL 13

    Expert Comment

    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.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now