How to check the Windows OS registry if a software is installed in the system using C#

Many a times it might be required that the Windows OS registry be checked to find out if a software is installed. we can do this very easily using C# as follows.

using System;
using System.Text;
using Microsoft.Win32;

namespace Logiphix
{
    class Program
    {
         static void Main(string[] args)
         {
                bool isInstalled = false;
                string registry_key = @"SOFTWARE\7-Zip";
                using (Microsoft.Win32.RegistryKey key = Registry.CurrentUser.OpenSubKey(registry_key))
                {
                      if (key != null)
                      {
                               isInstalled = true
                               /* This is the place where you may want to put the code if the software is installed
                                  i am just setting a flag here to be true.
                               */
                      }
                      else
                      {
                               isInstalled = false;
                      }
                }

                Console.WriteLine(isInstalled ? "The software is installed" : "The Software is not installed");
                Console.ReadKey();
         }
     }
}

This is a simple console app, Here i am trying to check if the 7-ZIP software is installed in my local machine. You can use the regedit to find out the registry key for that particular software on a machine where it is already installed and then use it in your code as above.

To use Regedit type regedit in the run window the following window will open




Copy the registry key as below.(as shown for 7-Zip , you can find the required software key by just navigating as below and then right click on the folder and select Copy Key Name


To use the copied key name:
For example if the current key name is as  for 7-Zip    HKEY_CURRENT_USER\Software\7-Zip
use only the string from       Software\       followed by the remaining string.
The    HKEY_CURRENT_USER\    can be discarded. 
Use a little of trial and error for your requirements

For example:   string registry_key = @"SOFTWARE\7-Zip";

This Should do the job for you.




No comments:

Post a Comment