2011-08-26 74 views
1

我試圖從特定已安裝應用程序(如安裝文件夾,卸載字符串,版本號等等)中檢索數據。當我運行以下代碼時,我獲取安裝文件夾,但它會爲UninstallString值返回四行問號。有任何想法嗎?如何獲取已安裝程序的卸載字符串

public static void FindInstalled(string AppName) 
    { 
     StringBuilder sbProductCode = new StringBuilder(39); 

     int iIdx = 0; 

     while (
      0 == MsiEnumProducts(iIdx++, sbProductCode)) 
     { 
      Int32 productNameLen = 512; 
      StringBuilder sbProductName = new StringBuilder(productNameLen); 

      MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen); 

      if (sbProductName.ToString().Contains(AppName)) 
      { 
       Int32 installDirLen = 1024; 
       StringBuilder sbInstallDir = new StringBuilder(installDirLen); 

       MsiGetProductInfo(sbProductCode.ToString(), "InstallLocation", sbInstallDir, ref installDirLen); 
       Console.Writeline("Install Directory - {0}",sbInstallDir.ToString()); 

       MsiGetProductInfo(sbProductCode.ToString(), "UninstallString", sbInstallDir, ref installDirLen); 
       Console.Writeline("Uninstall String - {0}", sbInstallDir.ToString()); 

      } 
     } 
    } 

回答

1

UninstallString不是有效的屬性。有關有效屬性的列表,請參見http://msdn.microsoft.com/en-us/library/aa370130(VS.85).aspx

如果您打開Windows Installer頭文件(「msi.h」)並搜索文本「UninstallString」,您將無法找到它。另外,如果您查看http://msdn.microsoft.com/en-us/library/aa370905(VS.85).aspx的屬性參考並搜索該頁面的「UninstallString」,您也不會找到它。

我的建議是讀取註冊表中的屬性。有關詳細信息,請參見http://msdn.microsoft.com/en-us/library/aa372105(VS.85).aspx。你可以從中得到你需要的細節。

0

怎麼是這樣的:

public static void FindInstalled(AppName) 
    { 
     RegistryKey myRegKey = Registry.LocalMachine; 
     myRegKey = myRegKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"); 
     String[] subkeyNames = myRegKey.GetSubKeyNames(); 
     foreach (String s in subkeyNames) 
     { 
      RegistryKey UninstallKey = Registry.LocalMachine; 
      UninstallKey = UninstallKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + s); 
      Object oValue = UninstallKey.GetValue("DisplayName"); 

      if (oValue != null) 
      { 
       if (oValue.ToString() == AppName) 
       { 
        oValue = UninstallKey.GetValue("UninstallString"); 
        Console.Writeline("Uninstall URL - {0}", oValue.ToString()); 
        break; 
       } 
      } 
     } 
    } 
相關問題