Get uninstall application path by name issue



  • I want to get application uninstall path by app's name from registry.

    Code:

    QString Test::getAppUninstallPath(QString name)
    {
        QString uninstallLocation;
        QStringList allCurrentUserKeys;
        QSettings registryKeyCurrentUser("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", QSettings::NativeFormat);
        allCurrentUserKeys = registryKeyCurrentUser.allKeys();
    
        for (QString key : allCurrentUserKeys) {
            if (key.contains("DisplayName")) {
                if (registryKeyCurrentUser.value(key).toString() == name) {
                    uninstallLocation = registryKeyCurrentUser.value("UninstallString").toString();
                }
            }
        }
    
        return uninstallLocation;
    }
    

    It returns nothing. How to get the app's uninstall path? Thanks.



  • @Cobra91151

    Had you try to debug and see in which part is not working as was expected?

    Thank you.


  • Moderators

    @Cobra91151 You need to check each step if you're expecting different behavior.

    I.e. add a check to see if allCurrentUserKeys.isEmpty(). Then add an else for if (key.contains("DisplayName") and finally add an else to the last if (registerKey ...) line.

    In each of those cases just add a qDebug(). Then you can track what is failing which will help you figure out why.



  • Hi, it's slightly more complicated, because you're dealing with hierarchies/nested keys, so easiest is to use the beginGroup()//endGroup() guys to declutter the code, say like this:

    ...
    QString uninstallLocation;
    QSettings registryKeyLM("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", QSettings::NativeFormat);
    
    auto allGroups = registryKeyLM.childGroups();
    for (auto group : allGroups)
    {
        registryKeyLM.beginGroup(group);
    
        for (auto key : registryKeyLM.childKeys())
            if ("DisplayName" == key)
                if (registryKeyLM.value(key) == name)
                    uninstallLocation = registryKeyLM.value("UninstallString").toString();
    
        registryKeyLM.endGroup();
    }
    ...
    

    Also, note that in HKEY_CURRENT_USER... you'll only find a handful of entries, the real McCoy you'll find in HKEY_LOCAL_MACHINE.... (on my Windows7 HKCU... contains 3 entries but there are 299 entries in HKLM..) So I changed the code to HKLM...



  • @hskoglund

    It works. Thank you.


  • Moderators

    @Cobra91151 So basically your code worked fine you just didn't have any applications installed for the current user only. That would have been the reason for the test to see if allCurrentUserKeys.isEmpty() was true. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.