Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project in C# where i need to find the paths of all the software installed starting with a same prefix name.
For example , if i have 10 software's installed in my computer and 5 of them start with the prefix name say "Microsoft" and then followed by some other suffixes, then i need to populate all the 5 software's starting with the name "Microsoft" and their installation path.

I am able to identify only 1 software at a time but not multiple and i cannot locate the path of the installation.
Any help will be of great motivation.
Thanks in advance!!!

C#
public  bool IsProgramInstalled(string programDisplayName)
        {

            Console.WriteLine(string.Format("Checking install status of: {0}", programDisplayName));
            foreach (var item in Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall").GetSubKeyNames())
            {

                object programName = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + item).GetValue("DisplayName");

                Console.WriteLine(programName);

                if (string.Equals(programName, programDisplayName))
                {
                    Console.WriteLine("Install status: INSTALLED");
                    return true;
                }
            }
            Console.WriteLine("Install status: NOT INSTALLED");
            return false;
        }
Posted
Updated 23-Apr-14 1:33am
v2
Comments
[no name] 23-Apr-14 7:30am    
Help with what?
ZurdoDev 23-Apr-14 7:30am    
If you have code that is sort of working it would be helpful to add it to your question.
sovit agarwal 23-Apr-14 7:34am    
Ryan please check the code added.I have added it now
sovit agarwal 23-Apr-14 7:34am    
Wes : Help with the question mentioned "Finding path of all installed software in C# having the same prefix name"
[no name] 23-Apr-14 8:24am    
That is not a question or any kind of a problem description. You are not keeping a list of the items found, you are only looking for one specific item and then returning if found. You are not looking for a group of anything. You are only returning a bool nothing to do with the path of items found. In short, this code presented does not even attempt to do anything you said you was trying.

First, I think you're copying and pasting code you found on the Internet and don't understand what it does or how it works.

The InstallLocation you're looking (in the registry) for does NOT have to be filled in, and in most cases isn't. This means that you're not going to be able to find where a package is installed for a lot of installations. That information, sadly, is coded into a lot of installers and there's no way to reliably get that information out of the installer.
 
Share this answer
 
Hi Member,

I think Dave Kreskowiak is correct that your approach won't catch all installed software reliably.
Anyway if you want to try this approach: here is some code that does the seaching part you asked for. (I don't think you will get the associated install paths easily, I even didn't try.., but the example code could be changed to give something else back than just the Display names...)

C#
using System;
using System.Collections.Generic;
using Microsoft.Win32;

namespace GetInstalledSoftware
{
    class Program
    {
        static void Main(string[] args)
        {
            // Search for all installed software starting with "Microsoft"
            string[] astrMatches = GetAllInstalledSoftware("Microsoft");

            foreach (string strDisplayName in astrMatches)
            {
                Console.WriteLine(strDisplayName);
            }

            Console.ReadKey();
        }

        static string[] GetAllInstalledSoftware(string strPrefix)
        {
            const string strUNINSTALL_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            List<string> listMatches = new List<string>();

            // Enumerate all sub keys found under the "Uninstall" key, each sub key represents a installed software
            foreach (string strSubKey in Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY).GetSubKeyNames())
            {
                // try to get the "DisplayName" for the installed software
                object objValue = Registry.LocalMachine.OpenSubKey(strUNINSTALL_KEY + @"\" + strSubKey).GetValue("DisplayName");
                if (objValue != null)
                {
                    string strDisplayName = objValue.ToString();

                    // If display name starts with the desired prefix
                    if (strDisplayName.StartsWith(strPrefix))
                    {
                        // -> add it to the result list
                        listMatches.Add(strDisplayName);
                    }
                }
            }
            return listMatches.ToArray();
        }
    }
}


But be aware that I don't think this is a "good" solution: reading registry, relying on the "DisplayName" property (which isn't always set), relying on the prefix (not seaching inside the Display string) etc. Anyway, I hope you don't use that code for any real world scenario (It seemed you even had problems to understand the example code you gave - or copied... So my general advice for beginners: leave the registry alone :-)

If you have any further questions - feel free to ask.

Kind regards

Johannes
 
Share this answer
 
v2
Comments
sovit agarwal 24-Apr-14 4:53am    
Thanks Johannes!!!
But may i know if there is any other way apart from using Registry to know the applications installed and their paths. The point is I have to use this code for implementing a Client Server real world application in C# and now i clearly understand that this code cannot be used for real world apps.
If you know of any other method, then do suggest!!!
Thanks again...
johannesnestler 24-Apr-14 8:35am    
Hi,
You are welcome :-) -> Yes, this code is not "bad" for itself but not the way for (reliable) detecting installed applications (or their pahts!). Sounds like you are facing a "bigger" problem, sorry I have no idea how to get the installed software in a more reliable way. Maybe you can narrow it down to specific install processes or Software vendors, something that gives you more Information how to recognize a Software as "installed". Maybe it is a at least a little help for you that you found a way how to not do it ;-) Anyway good luck with your project!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900