Code Examples

Get list of installed programs

programming

The following code examples will show you how to get the list of installed applications in Windows OS.

Code in C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Need to install package from NuGet
// NuGet\Install-Package System.ServiceProcess.ServiceController -Version 7.0.0
using System.ServiceProcess;
// NuGet\Install-Package System.Management -Version 7.0.0
using System.Management;

        public static void LogInstalledSoftware()
        {
            var line = string.Format(FORMAT, "DisplayName", "Version", "Publisher", "InstallDate");
            line += string.Format(FORMAT, "-----------", "-------", "---------", "-----------");
            var sb = new StringBuilder(line, 100000);
            ReadRegistryUninstall(ref sb, RegistryView.Registry32);
            sb.Append($"\n[64 bit section]\n\n{line}");
            ReadRegistryUninstall(ref sb, RegistryView.Registry64);
            File.WriteAllText(@"d:\projects\csharp\log.txt", sb.ToString());
        }

        private static void ReadRegistryUninstall(ref StringBuilder sb, RegistryView view)
        {
            const string REGISTRY_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
            using var subKey = baseKey.OpenSubKey(REGISTRY_KEY);
            foreach (string subkey_name in subKey.GetSubKeyNames())
            {
                using RegistryKey key = subKey.OpenSubKey(subkey_name);
                if (!string.IsNullOrEmpty(key.GetValue("DisplayName") as string))
                {
                    var line = string.Format(FORMAT,
                        key.GetValue("DisplayName"),
                        key.GetValue("DisplayVersion"),
                        key.GetValue("Publisher"),
                        key.GetValue("InstallDate"));
                    sb.Append(line);
                }
                key.Close();
            }
            subKey.Close();
            baseKey.Close();
        }

Code in Python

import psutil
from windows_tools.installed_software import get_installed_software
import wmi

def getInstalledPrograms():

    for software in get_installed_software():
        print(software['name'], software['version'], software['publisher'])