【问题标题】:Correct method to get installed application path by app. name in C#通过应用程序获取安装的应用程序路径的正确方法。 C#中的名字
【发布时间】:2012-04-04 23:41:42
【问题描述】:

我使用此功能查找任何已安装的应用程序

但参数“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);
             }
       }
}

【问题讨论】:

    标签: c# .net windows path installation


    【解决方案1】:

    我访问过以下链接,它们似乎并不过时:

    我看到的唯一可以使用的键是这些:

    • ARPINSTALLLOCATION
    • 安装目录
    • INSTALLPROPERTY_INSTALLLOCATION
    • 安装位置

    我应该指出,似乎应该使用MsiGetProductInfoEx(第二个链接)来收集有关其他用户添加的已发布/已安装产品的信息;并且需要管理权限。

    【讨论】:

      【解决方案2】:

      我找到了其他解决方案,效果很好。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-19
        • 2017-05-06
        • 2012-06-25
        • 1970-01-01
        • 2018-09-25
        相关资源
        最近更新 更多