Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
Project detectProgram
Purpose
The purpose of this tutorial is to show how to use a C# .Net 4.7 windows application to find the installation path of a program on your computer. With this tutorial you can get a list of all programs installed along with locations or the same sort of list for exact or similarly named programs.

Since installation methods vary, the keys that are looked for may or may not have the installation location of a program.

Download detectProgram.exe (in zip file)
Download detectProgram source code (in zip file)

Core Functionality of The Application Is Shown Below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;

namespace detectProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            string installedProgramToSearchFor = textBox1.Text.Trim().Replace("'", "''").Replace("\"", "").Replace(";", "").Replace("%", "").Replace("--", "");
            textBox2.Text = "Working...";

            var collectedResults = new List<string>();
            var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Name, InstallLocation FROM Win32_Product WHERE Name LIKE '%" + installedProgramToSearchFor + "%'");
            /*
            Code snippet that will expose the name/value pairs if you do not have them already as only two are used here
            string finalResults = String.Empty;
            ManagementObjectCollection propKeyValues = searcher.Get();
            foreach (ManagementObject propCollData in propKeyValues)
            {
                foreach (PropertyData prop in propCollData.Properties)
                {
                    finalResults = finalResults + "Name=" + prop.Name + ", Value=" + prop.Value + Environment.NewLine;
                }
            }
            textBox2.Text = finalResults;
            */

            /* With name/values known, iterate through results.  It should be noted that not all installed programs will set the InstalledLocation value */
            foreach (ManagementObject searchItem in searcher.Get())
            {
                /* Name of Program */
                string installName = String.Empty;
                try
                {
                    installName = searchItem["Name"].ToString();
                }
                catch (Exception)
                {
                    installName = "Name Not Registered";
                }
                /* Installation Location of Program */
                string installLocation = String.Empty;
                try
                {
                    installLocation = searchItem["InstallLocation"].ToString();
                }
                catch (Exception)
                {
                    installLocation = "Filepath Not Registered";
                }

                string installedProgramLocation = "[" + installName + "] " + installLocation;
                collectedResults.Add(installedProgramLocation);
            }
            collectedResults.Sort();
            /* Convert sorted lisit to array */
            string[] collectedResultsArray = collectedResults.ToArray();
            /* Format data and add to the text box */
            string finalResults = String.Empty;
            for (int i = 0; i < collectedResultsArray.Length; i++)
            {
                finalResults = finalResults + collectedResultsArray[i] + Environment.NewLine;
            }
            textBox2.Text = finalResults;
            button1.Enabled = true;
        }
    }
}


About Joe
Find Out Now!