Bas van der Ploeg
Bas van der Ploeg

Reputation: 81

Execute programs from install location

First I scan for every installed program on the system. From this I get the InstallLocation and DisplayName. Now I would like to Execute the scanned programs from this Name or location. Is there a way to do this or do I really need the name or location of the executable?

public static class InstalledPrograms 
{    
    const string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";

    public static List<string> GetInstalledPrograms()
    {
        var result = new List<string>();
        var termsList = new List<string>();
        result.AddRange(GetInstalledProgramsFromRegistry(RegistryView.Registry32));
        result.AddRange(GetInstalledProgramsFromRegistry(RegistryView.Registry64));
        result = result.Distinct().ToList();
        result.Sort();
        foreach (var i in result)
        {
            Console.WriteLine(i);

        }
        return result;
    }

    private static IEnumerable<string> GetInstalledProgramsFromRegistry(RegistryView registryView)
    {
        var result = new List<string>();

        using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(registry_key))
        {
            foreach (string subkey_name in key.GetSubKeyNames())
            {
                using (RegistryKey subkey = key.OpenSubKey(subkey_name))
                {
                    if (IsProgramVisible(subkey))
                    {
                        var ii = (string)subkey.GetValue("InstallLocation");
                        result.Add(ii);
                    }
                }
            }
        }

        return result;
    }

    private static bool IsProgramVisible(RegistryKey subkey)
    {
        var name = (string)subkey.GetValue("DisplayName");
        var releaseType = (string)subkey.GetValue("ReleaseType");
        //var unistallString = (string)subkey.GetValue("UninstallString");
        var systemComponent = subkey.GetValue("SystemComponent");
        var parentName = (string)subkey.GetValue("ParentDisplayName");

        return
            !string.IsNullOrEmpty(name)
            && string.IsNullOrEmpty(releaseType)
            && string.IsNullOrEmpty(parentName)
            && (systemComponent == null);
    }
}

Upvotes: 0

Views: 72

Answers (1)

Berin Loritsch
Berin Loritsch

Reputation: 11463

The short answer is no you can't find the application path using that part of the registry tree. That information is install dependent. You can find the uninstaller since that path is provided. I seriously doubt that users of your program would be happy with you invoking an uninstall of every application/hot fix installed.

The only thing you can do is to scan those directories for .exe files. If you only have one, then you might be OK. Of course, if they were installed with an installer, there's a good chance an uninstaller lives in the same directory.

There's just no predictable way to simply launch every installed application. Sometimes the installed objects are libraries and extensions to existing products.

Another thing to consider is that store apps have new rules surrounding how they are installed/uninstalled and even invoked. Windows store apps don't have a ".exe" file you can simply invoke even if you could find it.

Upvotes: 1

Related Questions