Search code examples
c#wpfvisual-studiowindows-10dllimport

List all installed programs into a Context </MenuItem>


I'm trying to create a drop-down menu that shows the installed programs for the current user that's logged into Windows. Perhaps if not .Net, can I obtain them from a DLLImport dll?

edit: ListView items as List< T > could be also a great option, I just need to obtain the list of Installed programs (and set the ListView as a context drop-down menu). enter image description here


Solution

  • Without further ahelp, I have figured out myself that you can use the Registry class in C# to access the Windows registry and retrieve the list of installed programs for the current user. The registry key you will need to access is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and you can iterate through the subkeys to get the list of installed programs.

    Here's the example:

    List<string> installedPrograms = new List<string>();
    using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
    {
        foreach (string subkeyName in key.GetSubKeyNames())
        {
            using (RegistryKey subkey = key.OpenSubKey(subkeyName))
            {
                if (subkey.GetValue("DisplayName") != null)
                {
                    installedPrograms.Add(subkey.GetValue("DisplayName").ToString());
                }
            }
        }
    }
    

    And now the installedPrograms list can be used to populate the items of any drop-down menu.

    Regarding the second question, It is possible use DllImport to import the native functions from the appropriate DLL and then use the imported functions to retrieve the list of installed programs. However, using the Registry class is a simpler and more straightforward way to achieve this.