2017-07-23 61 views
-1

我想从注册表中获取应用程序名称的应用程序卸载路径。按名称问题获取卸载应用程序路径

代码:

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; 
} 

它没有返回。如何获取应用程序的卸载路径?谢谢。

回答

0

感谢hskoglund。因为我正在处理层次结构/嵌套键,所以我应该使用beginGroup()/endGroup()

代码:

QString uninstallLocation; 
    QSettings registryKeyLM("HKEY_CURRENT_USER\\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(); 
    } 

    return uninstallLocation;