Microsoft Developer Network > Forums Home > Visual Basic Forums > Visual Basic General > How can i detect witch frameworks are installed in a machine

Answered How can i detect witch frameworks are installed in a machine

  • Friday, February 12, 2010 6:17 PM
     
     
    Hi,
    I'm looking on the Internet but not found anything about how to detect what the frameworks are installed. Someone I can provide that code to VB. Net?
    TcoUpLoad (work with Vb6 and VbNet)

Answers

  • Friday, February 12, 2010 6:36 PM
     
     Answered
    Hi,
    I'm looking on the Internet but not found anything about how to detect what the frameworks are installed. Someone I can provide that code to VB. Net?
    TcoUpLoad (work with Vb6 and VbNet)

    There is a code written in C Sharp, how to detect what frameworks are installed in your machine.

    the project is here: http://www.codeproject.com/KB/dotnet/frameworkversiondetection.aspx


    For you to convert: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Just Be Humble Malange!
  • Friday, February 12, 2010 10:00 PM
     
     Answered Has Code
    See if the below code works for you. I converted it and made some mods from the below link:

    http://www.agusblog.com/wordpress/c-code-to-determine-which-version-of-the-net-framework-is-installed-177.htm

    Dim DotNetFrameworkInfo As New DotNetFramework.SystemInfo
    Console.WriteLine(DotNetFrameworkInfo.HighestFrameworkVersion)
    Console.WriteLine(DotNetFrameworkInfo.NetFrameworkInstallationPath)
    Dim FrameworkVersionCollection As New Collection
    FrameworkVersionCollection = DotNetFrameworkInfo.FrameworkVersions
    For Each FrameworkVersion As String In FrameworkVersionCollection
    	Console.WriteLine(FrameworkVersion)
    Next
    
    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Text.RegularExpressions
    
    Namespace DotNetFramework
        Public Class SystemInfo
            Private Const FRAMEWORK_PATH As String = "\Microsoft.NET\Framework"
            Private Const WINDIR1 As String = "windir"
            Private Const WINDIR2 As String = "SystemRoot"
            Private _versions As New Collection
    
            Public ReadOnly Property FrameworkVersions() As Collection
                Get
                    Try
                        GetVersions(NetFrameworkInstallationPath)
                    Catch e1 As SecurityException
                        Return Nothing
                    End Try
                    Return _versions
                End Get
            End Property
    
            Public ReadOnly Property HighestFrameworkVersion() As String
                Get
                    Try
                        Return GetHighestVersion(NetFrameworkInstallationPath)
                    Catch e1 As SecurityException
                        Return "Unknown"
                    End Try
                End Get
            End Property
    
            Private Sub GetVersions(ByVal installationPath As String)
    
                Dim versions() As String = Directory.GetDirectories(installationPath, "v*")
                Dim VersionNumber As String
                _versions.Clear()
    
                For i As Integer = versions.Length - 1 To 0 Step -1
                    VersionNumber = ExtractVersion(versions(i))
    
                    If IsFrameworkVersionFormat(VersionNumber) Then
                        _versions.Add(VersionNumber)
                    End If
                Next i
    
            End Sub
    
            Private Function GetHighestVersion(ByVal installationPath As String) As String
                Dim versions() As String = Directory.GetDirectories(installationPath, "v*")
                Dim version As String = "Unknown"
    
                For i As Integer = versions.Length - 1 To 0 Step -1
                    version = ExtractVersion(versions(i))
    
                    If IsFrameworkVersionFormat(version) Then
                        Return version
                    End If
                Next i
    
                Return version
            End Function
    
            Private Function ExtractVersion(ByVal directory As String) As String
                Dim startIndex As Integer = directory.LastIndexOf("\") + 2
                Return directory.Substring(startIndex, directory.Length - startIndex)
            End Function
    
            Private Shared Function IsFrameworkVersionFormat(ByVal str As String) As Boolean
                Return New Regex("^[0-9]+\.?[0-9]+\.?[0-9]*$").IsMatch(str)
            End Function
    
            Public ReadOnly Property NetFrameworkInstallationPath() As String
                Get
                    Return WindowsPath & FRAMEWORK_PATH
                End Get
            End Property
    
            Public ReadOnly Property WindowsPath() As String
                Get
                    Dim winDir As String = Environment.GetEnvironmentVariable(WINDIR1)
                    If String.IsNullOrEmpty(winDir) Then
                        winDir = Environment.GetEnvironmentVariable(WINDIR2)
                    End If
    
                    Return winDir
                End Get
            End Property
        End Class
    End Namespace
    
    

    Paul ~~~~ Microsoft MVP (Visual Basic)
  • Saturday, February 13, 2010 9:21 AM
     
     Answered Has Code
    Hi Malange,
    Take This very simple code:
    "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\vbc.exe") = True Then
                'write here the code when the program found the frame work v1.0
            Else
                'write here the code when the program didn't find the frame work v1.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe") = True Then
                'write here the code when the program found the frame work v1.1
            Else
                'write here the code when the program didn't find the frame work v1.1
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe") = True Then
                'write here the code when the program found the frame work v2.0
            Else
                'write here the code when the program didn't find the frame work v2.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v3.0\vbc.exe") = True Then
                'write here the code when the program found the frame work v3.0
            Else
                'write here the code when the program didn't find the frame work v3.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v3.5\vbc.exe") = True Then
                'write here the code when the program found the frame work v3.5
            Else
                'write here the code when the program didn't find the frame work v3.5
            End If
    
    
    
    
     You can change the path of the place that's the program search in by changing "c:\windows" to another path or to "%windir%"

    Don't forget to mark this post as answer if this post help you
    Mohamed Elghamry

All Replies

  • Friday, February 12, 2010 6:36 PM
     
     Answered
    Hi,
    I'm looking on the Internet but not found anything about how to detect what the frameworks are installed. Someone I can provide that code to VB. Net?
    TcoUpLoad (work with Vb6 and VbNet)

    There is a code written in C Sharp, how to detect what frameworks are installed in your machine.

    the project is here: http://www.codeproject.com/KB/dotnet/frameworkversiondetection.aspx


    For you to convert: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Just Be Humble Malange!
  • Friday, February 12, 2010 10:00 PM
     
     Answered Has Code
    See if the below code works for you. I converted it and made some mods from the below link:

    http://www.agusblog.com/wordpress/c-code-to-determine-which-version-of-the-net-framework-is-installed-177.htm

    Dim DotNetFrameworkInfo As New DotNetFramework.SystemInfo
    Console.WriteLine(DotNetFrameworkInfo.HighestFrameworkVersion)
    Console.WriteLine(DotNetFrameworkInfo.NetFrameworkInstallationPath)
    Dim FrameworkVersionCollection As New Collection
    FrameworkVersionCollection = DotNetFrameworkInfo.FrameworkVersions
    For Each FrameworkVersion As String In FrameworkVersionCollection
    	Console.WriteLine(FrameworkVersion)
    Next
    
    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Text.RegularExpressions
    
    Namespace DotNetFramework
        Public Class SystemInfo
            Private Const FRAMEWORK_PATH As String = "\Microsoft.NET\Framework"
            Private Const WINDIR1 As String = "windir"
            Private Const WINDIR2 As String = "SystemRoot"
            Private _versions As New Collection
    
            Public ReadOnly Property FrameworkVersions() As Collection
                Get
                    Try
                        GetVersions(NetFrameworkInstallationPath)
                    Catch e1 As SecurityException
                        Return Nothing
                    End Try
                    Return _versions
                End Get
            End Property
    
            Public ReadOnly Property HighestFrameworkVersion() As String
                Get
                    Try
                        Return GetHighestVersion(NetFrameworkInstallationPath)
                    Catch e1 As SecurityException
                        Return "Unknown"
                    End Try
                End Get
            End Property
    
            Private Sub GetVersions(ByVal installationPath As String)
    
                Dim versions() As String = Directory.GetDirectories(installationPath, "v*")
                Dim VersionNumber As String
                _versions.Clear()
    
                For i As Integer = versions.Length - 1 To 0 Step -1
                    VersionNumber = ExtractVersion(versions(i))
    
                    If IsFrameworkVersionFormat(VersionNumber) Then
                        _versions.Add(VersionNumber)
                    End If
                Next i
    
            End Sub
    
            Private Function GetHighestVersion(ByVal installationPath As String) As String
                Dim versions() As String = Directory.GetDirectories(installationPath, "v*")
                Dim version As String = "Unknown"
    
                For i As Integer = versions.Length - 1 To 0 Step -1
                    version = ExtractVersion(versions(i))
    
                    If IsFrameworkVersionFormat(version) Then
                        Return version
                    End If
                Next i
    
                Return version
            End Function
    
            Private Function ExtractVersion(ByVal directory As String) As String
                Dim startIndex As Integer = directory.LastIndexOf("\") + 2
                Return directory.Substring(startIndex, directory.Length - startIndex)
            End Function
    
            Private Shared Function IsFrameworkVersionFormat(ByVal str As String) As Boolean
                Return New Regex("^[0-9]+\.?[0-9]+\.?[0-9]*$").IsMatch(str)
            End Function
    
            Public ReadOnly Property NetFrameworkInstallationPath() As String
                Get
                    Return WindowsPath & FRAMEWORK_PATH
                End Get
            End Property
    
            Public ReadOnly Property WindowsPath() As String
                Get
                    Dim winDir As String = Environment.GetEnvironmentVariable(WINDIR1)
                    If String.IsNullOrEmpty(winDir) Then
                        winDir = Environment.GetEnvironmentVariable(WINDIR2)
                    End If
    
                    Return winDir
                End Get
            End Property
        End Class
    End Namespace
    
    

    Paul ~~~~ Microsoft MVP (Visual Basic)
  • Saturday, February 13, 2010 9:21 AM
     
     Answered Has Code
    Hi Malange,
    Take This very simple code:
    "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\vbc.exe") = True Then
                'write here the code when the program found the frame work v1.0
            Else
                'write here the code when the program didn't find the frame work v1.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\vbc.exe") = True Then
                'write here the code when the program found the frame work v1.1
            Else
                'write here the code when the program didn't find the frame work v1.1
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe") = True Then
                'write here the code when the program found the frame work v2.0
            Else
                'write here the code when the program didn't find the frame work v2.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v3.0\vbc.exe") = True Then
                'write here the code when the program found the frame work v3.0
            Else
                'write here the code when the program didn't find the frame work v3.0
            End If
            If My.Computer.FileSystem.FileExists("C:\WINDOWS\Microsoft.NET\Framework\v3.5\vbc.exe") = True Then
                'write here the code when the program found the frame work v3.5
            Else
                'write here the code when the program didn't find the frame work v3.5
            End If
    
    
    
    
     You can change the path of the place that's the program search in by changing "c:\windows" to another path or to "%windir%"

    Don't forget to mark this post as answer if this post help you
    Mohamed Elghamry