Skip to content

Instantly share code, notes, and snippets.

@Sheepings
Created January 9, 2020 22:56
Show Gist options
  • Save Sheepings/e557e1f7bbb3adfeb8e4215c1395dc70 to your computer and use it in GitHub Desktop.
Save Sheepings/e557e1f7bbb3adfeb8e4215c1395dc70 to your computer and use it in GitHub Desktop.
Getting All Installed Programs From Windows Registry Using Multi-Threading
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace TestCSharpApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MainMethod();
}
public List<string> result = new List<string>();
public BackgroundWorker bgWorker = new BackgroundWorker();
public delegate void DelegateCallback(string s);
private void MainMethod()
{
bgWorker.DoWork += BgWorker_DoWork;
bgWorker.RunWorkerCompleted += BgWorker_RunWorkerCompleted;
bgWorker.RunWorkerAsync();
}
private void BgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
bgWorker.DoWork -= BgWorker_DoWork;
bgWorker.RunWorkerCompleted -= BgWorker_RunWorkerCompleted;
}
private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{
result = Get_Installed_Software.GetInstalled_Programs();
if (result != null)
{
result.ForEach((string item) => listBox1.Invoke(new DelegateCallback(UpdateUI),
new string[] { item })); /* Use string.split to split at "," */
}
}
private void UpdateUI(string item)
{
listBox1.Items.Add(item);
}
}
public static class Get_Installed_Software
{
private const string reg_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
public static List<string> GetInstalled_Programs()
{
List<string> program_Values = new List<string>();
program_Values.AddRange(ReadProgram_Instalations(RegistryView.Registry32));
program_Values.AddRange(ReadProgram_Instalations(RegistryView.Registry64));
return program_Values;
}
private static IEnumerable<string> ReadProgram_Instalations(RegistryView registryView)
{
List<string> program_Values = new List<string>();
using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(reg_key))
{
for (int i = 0; i < key.GetSubKeyNames().Length; i++)
{
using (RegistryKey subkey = key.OpenSubKey(key.GetSubKeyNames()[i]))
{
if (All_KeyValues_Exist(subkey))
{
program_Values.Add(string.Concat((string)subkey.GetValue("DisplayName"), ", ", (string)subkey.GetValue("Publisher"), ", ", (string)subkey.GetValue("InstallLocation")));
}
}
}
}
return program_Values;
}
private static bool All_KeyValues_Exist(RegistryKey subkey)
{
string app_Name = (string)subkey.GetValue("DisplayName");
string publisher_Name = (string)subkey.GetValue("Publisher");
string installLocation = (string)subkey.GetValue("InstallLocation");
if (string.IsNullOrEmpty(app_Name) || string.IsNullOrEmpty(publisher_Name) || string.IsNullOrEmpty(installLocation))
return false;
else
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment