Determining if the .NET Framework Is Installed

Introduction

Every now and then, we must do something through code that could have been done without code. This may sound strange, but having a project at the ready can save you time. In the example today, we are going to create a project that interrogates the Registry for the presence of the .NET Framework, as well as its version.

As mentioned, we can find this information in the Registry, but it does take some time, and some users do not have access to the Registry Editor.

You may argue that this type of information is also available if we scratch around among the user’s folders, but, again, there might be a permission issue or a security issue that prevents us from doing just that.

In today’s article, you will learn how to use the Registry to determine if the .NET Framework is present on a user’s machine, as well as which .NET Framework version is installed.

Practical

Start Visual Studio and create either a Visual Basic.NET or a C# Windows Forms project. Once the form is loaded, design your screen to look like Figure 1.

Design
Figure 1: Design

The Form contains two buttons and one ListBox. The first button will be used to determine .NET Framework 1 to .Net Framework 4. The next button is used to determine a .NET Framework of 4.5 and higher.

Code

As always, add the Namespaces you will use.

C#

using Microsoft.Win32;
using System;
using System.Windows.Forms;

VB.NET

Imports Microsoft.Win32
Imports System

Add the code for the first button. This code interrogates the Registry to find the versions of the .NET Framework (from 1 to 4) installed as well as their respective Service Packs. Some string manipulation is done, and the results are added to the ListBox.

C#

   private void NETOneToFour()
   {
      using (RegistryKey netKey = RegistryKey.OpenBaseKey
         (RegistryHive.LocalMachine, RegistryView.Registry32)
         .OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup
         \NDP\"))
      {

         foreach (var versionKeyName in netKey.GetSubKeyNames())
         {

            if (versionKeyName == "v4")
            {

               continue;

            }

            if (versionKeyName.StartsWith("v"))
            {

               RegistryKey verKey = netKey.OpenSubKey
                  (versionKeyName);

               var strVer = (string)verKey.GetValue("Version",
                  "");

               var strServicePack = verKey.GetValue("SP",
                  "").ToString();

               var strInstalled = verKey.GetValue("Install",
                  "").ToString();

               if (string.IsNullOrEmpty(strInstalled))

                  listBox1.Items.Add($"{versionKeyName}
                     {strVer}");

               else
               {

                  if (!(string.IsNullOrEmpty(strServicePack)) &&
                     strInstalled == "1")
                  {

                     listBox1.Items.Add($"{versionKeyName}
                        {strVer}  Service Pack{strServicePack}");

                  }
               }

               if (!string.IsNullOrEmpty(strVer))
               {

                  continue;

               }

               foreach (var strSubKeys in
                  verKey.GetSubKeyNames())
               {

                  RegistryKey subKey =
                     verKey.OpenSubKey(strSubKeys);

                  strVer = (string)subKey.GetValue("Version", "");

                  if (!string.IsNullOrEmpty(strVer))
                     strServicePack = subKey.GetValue("SP",
                        "").ToString();

                     strInstalled = subKey.GetValue("Install",
                        "").ToString();

                  if (string.IsNullOrEmpty(strInstalled))

                     listBox1.Items.Add($"{versionKeyName}
                        {strVer}");

                  else
                  {

                     if (!(string.IsNullOrEmpty(strServicePack)) &&
                        strInstalled == "1")
                     {

                        listBox1.Items.Add($"{strSubKeys}  {strVer}
                           Service Pack{strServicePack}");

                     }

                     else if (strInstalled == "1")
                     {

                        listBox1.Items.Add($"  {strSubKeys}
                           {strVer}");

                     }

                  }

               }

            }

         }

      }

   }
   private void button1_Click(object sender, EventArgs e)
   {

      NETOneToFour();

   }

VB.NET

   Private Sub NETOneToFour()

      Using netKey As RegistryKey = RegistryKey.OpenBaseKey _
            (RegistryHive.LocalMachine, RegistryView.Registry32) _
            .OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup _
            \NDP\")

         For Each versionKeyName In netKey.GetSubKeyNames()

            If versionKeyName = "v4" Then

               Continue For

            End If

            If versionKeyName.StartsWith("v") Then

               Dim verKey As RegistryKey = netKey.OpenSubKey _
                  (versionKeyName)

               Dim strVer = CStr(verKey.GetValue("Version", ""))
               Dim strServicePack = verKey.GetValue("SP", "") _
                  .ToString()
               Dim strInstalled = verKey.GetValue("Install", "") _
                  .ToString()

               If String.IsNullOrEmpty(strInstalled) Then

                  listBox1.Items.Add($"{versionKeyName}  {strVer}")

               Else


                  If Not (String.IsNullOrEmpty(strServicePack)) _
                     AndAlso strInstalled = "1" Then

                     listBox1.Items.Add($"{versionKeyName}  _
                        {strVer}  Service Pack{strServicePack}")

                  End If

               End If

               If Not String.IsNullOrEmpty(strVer) Then

                  Continue For

               End If

               For Each strSubKeys In verKey.GetSubKeyNames()

                  Dim subKey As RegistryKey = verKey.OpenSubKey _
                     (strSubKeys)

                  strVer = CStr(subKey.GetValue("Version", ""))

                  If Not String.IsNullOrEmpty(strVer) Then _
                     strServicePack = subKey.GetValue("SP", "") _
                     .ToString()

                  strInstalled = subKey.GetValue("Install", "") _
                     .ToString()

                  If String.IsNullOrEmpty(strInstalled) Then

                     listBox1.Items.Add($"{versionKeyName}
                        {strVer}")

                  Else

                     If Not (String.IsNullOrEmpty(strServicePack)) _
                        AndAlso strInstalled = "1" Then

                        listBox1.Items.Add($"{strSubKeys}  {strVer} _
                           Service Pack{strServicePack}")

                     ElseIf strInstalled = "1" Then

                        listBox1.Items.Add($"  {strSubKeys} _
                           {strVer}")

                     End If

                  End If

               Next

            End If

         Next

      End Using

   End Sub
   Private Sub button1_Click(sender As Object, e As EventArgs) _
         Handles button1.Click

      NETOneToFour()

   End Sub

Add the code for the next button to identify any .NET Framework higher than .NET Framework 4.5.

C#

      private void NETFourDotFiveAndHigher()
      {

         const string strSubKey = @"SOFTWARE\Microsoft\NET
            Framework Setup\NDP\v4\Full\";

         using (var netKey = RegistryKey.OpenBaseKey(RegistryHive
            .LocalMachine, RegistryView.Registry32)
            .OpenSubKey(strSubKey))
         {

            if (netKey != null && netKey.GetValue("Release")
               != null)
            {

               listBox1.Items.Add(".NET Framework Version: " +
                  strHigher((int)netKey.GetValue("Release")));

            }

            else
            {

               listBox1.Items.Add(".NET Framework Version 4.5 or
                                   later not detected.");

            }
         }

         string strHigher(int intRelease)
         {
            if (intRelease >= 461808)

               return "4.7.2 or later";

            if (intRelease >= 461308)

               return "4.7.1";

            if (intRelease >= 460798)

               return "4.7";

            if (intRelease >= 394802)

               return "4.6.2";

            if (intRelease >= 394254)

               return "4.6.1";

            if (intRelease >= 393295)

               return "4.6";

            if (intRelease >= 379893)

               return "4.5.2";

            if (intRelease >= 378675)

               return "4.5.1";

            if (intRelease >= 378389)

               return "4.5";

               return "No 4.5 or later version detected";
            }

         }

         private void button2_Click(object sender, EventArgs e)
         {

            NETFourDotFiveAndHigher();

         }

VB.NET

   Private Sub NetFourDotFiveAndHigher()

      Const strSubKey As String = "SOFTWARE\Microsoft\NET _
         Framework Setup\NDP\v4\Full\"

      Dim netKey As RegistryKey = RegistryKey.OpenBaseKey _
         (RegistryHive.LocalMachine, RegistryView.Registry32) _
         .OpenSubKey(strSubKey)

      If netKey IsNot Nothing And netKey.GetValue("Release") _
            IsNot Nothing Then


         listBox1.Items.Add(".NET Framework Version: " + _
            strHigher(Integer.Parse(netKey.GetValue("Release"))))

      Else

         listBox1.Items.Add(".NET Framework Version 4.5 or later _
            not detected.")

      End If

   End Sub

   Private Function strHigher(ByVal intRelease As Integer) As String

   If intRelease >= 461808 Then Return "4.7.2 or later"

      If intRelease >= 461308 Then Return "4.7.1"

      If intRelease >= 460798 Then Return "4.7"

      If intRelease >= 394802 Then Return "4.6.2"

      If intRelease >= 394254 Then Return "4.6.1"

      If intRelease >= 393295 Then Return "4.6"

      If intRelease >= 379893 Then Return "4.5.2"

      If intRelease >= 378675 Then Return "4.5.1"

      If intRelease >= 378389 Then Return "4.5"

      Return "No 4.5 or later version detected"

   End Function

   Private Sub button2_Click(sender As Object, e As EventArgs) _
         Handles button2.Click

      NetFourDotFiveAndHigher()

   End Sub

Figures 2 and 3 displays the respective results.

.NET Framework 1 to 4
Figure 2: .NET Framework 1 to 4

.NET Framework 4.5 and Higher
Figure 3: .NET Framework 4.5 and Higher

Conclusion

Looking in the Registry is a quick and easy way to determine the existence of many applications, such as Microsoft office, Adobe Acrobat Reader, and obviously the .NET Framework. I hope you have enjoyed today’s article. Until next time, happy coding!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read