2012-03-20 178 views
0

我使用此函數來查找任何已安裝的應用程序通過應用程序獲取已安裝的應用程序路徑的正確方法。 C#中的名稱

但是參數「InstallLocation」完全不起作用。

任何線索?

謝謝!

void FindApplication(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 = 2048; 
      StringBuilder sbInstallDir = new StringBuilder(installDirLen); 
      MsiGetProductInfo(sbProductCode.ToString(),"InstallLocation", sbInstallDir, ref installDirLen); 

      string result = string.Format("ProductName {0}: {1}", sbProductName, sbInstallDir); 
      } 
     } 
} 

回答

3

我去過以下鏈接,他們似乎並沒有過時:

唯一的按鍵我看可以使用的是:

  • ARPINSTALLLOCATION
  • INSTALLDIR
  • INSTALLPROPERTY_INSTALLLOCATION
  • INSTALLLOCATION

我應該指出,它出現MsiGetProductInfoEx(第二連桿)應該被用於收集通過加入發佈/已安裝的產品信息另一個用戶;並要求行政權限。

+0

它們不能正常工作。 :( – 2012-03-20 13:55:09

+1

@Peretz我更新了我的文章,希望它可以幫助你更多。 – 2012-03-20 14:01:58

0

我發現其他解決方案,它工作正常。

string FindPathByInstalledAppEXEName(string appEXEName) 
{ 
    string path = string.Empty; 

    try 
    { 
     RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Installer\Assemblies"); 

     string regfilepath = string.Empty; 
     if (key != null) // Make sure there are Assemblies 
     { 
      foreach (string Keyname in key.GetSubKeyNames()) 
      { 
       if (Keyname.IndexOf(appEXEName) > 0) 
       { 
        regfilepath = Keyname; 
        break; 
       } 
      } 
     } 

     if (!string.IsNullOrEmpty(regfilepath)) 
     { 
      string fullpath = ""; 
      for (int a = 0; a < regfilepath.Length; a++) 
      { 
       if (regfilepath.IndexOf("|", a, 1) > 0) 
        fullpath += "\\"; 
       else 
        fullpath += regfilepath.Substring(a, 1); 
      } 
      path = fullpath.Substring(0, fullpath.LastIndexOf("\\") + 1); 
     } 
    } 
    catch // (Exception ex) 
    { 
    } 

    return path; 
} 
相關問題