Skip to content

Instantly share code, notes, and snippets.

@Sheepings
Created January 12, 2020 15:10
Show Gist options
  • Save Sheepings/099811c619511b352aa6a6b0aaae115e to your computer and use it in GitHub Desktop.
Save Sheepings/099811c619511b352aa6a6b0aaae115e to your computer and use it in GitHub Desktop.
This code will return the list of installed program names and the install path if it exists.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestCSharpApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MainMethod();
}
public delegate void DelegateCallback(string s);
private void MainMethod()
{
Task t = new Task(GetDependentObjects, TaskCreationOptions.LongRunning);
Thread tt = new Thread(GetDependentObjects, 0);
tt.Start();
}
private void GetDependentObjects()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["Name"] != null && queryObj["InstallLocation"] != null)
{
listBox1.Invoke(new DelegateCallback(UpdateUI), new string[] { string.Concat((string)queryObj["Name"], ", ", (string)queryObj["InstallDate"], ", ", (string)queryObj["InstallLocation"]) });
}
}
}
private void UpdateUI(string item)
{
listBox1.Items.Add(item);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment