C# 按应用程序获取已安装应用程序路径的正确方法。用C写的名字#

C# 按应用程序获取已安装应用程序路径的正确方法。用C写的名字#,c#,.net,windows,path,installation,C#,.net,Windows,Path,Installation,我使用此功能查找任何已安装的应用程序 但是参数“InstallLocation”根本不起作用 有线索吗 谢谢 void FindApplication(string appName) { StringBuilder sbProductCode = new StringBuilder(39); int iIdx = 0; while (0 == MsiEnumProducts(iIdx++, sbProductCode)) { Int32 produ

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

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

我访问了以下链接,它们似乎没有过时:

我看到的唯一可以使用的钥匙是:

  • ARP安装位置
  • INSTALLDIR
  • INSTALLPROPERTY\u INSTALLLOCATION
  • 安装地点

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

我访问了以下链接,它们似乎没有过时:

我看到的唯一可以使用的钥匙是:

  • ARP安装位置
  • INSTALLDIR
  • INSTALLPROPERTY\u INSTALLLOCATION
  • 安装地点

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

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

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;
}
string FindPathByInstalledAppEXEName(string appEXEName)
{
字符串路径=string.Empty;
尝试
{
RegistryKey key=Registry.CurrentUser.OpenSubKey(@“Software\Microsoft\Installer\Assembly”);
string regfilepath=string.Empty;
if(key!=null)//确保有程序集
{
foreach(key.GetSubKeyNames()中的字符串Keyname)
{
if(Keyname.IndexOf(appEXEName)>0)
{
regfilepath=Keyname;
打破
}
}
}
如果(!string.IsNullOrEmpty(regfilepath))
{
字符串fullpath=“”;
for(int a=0;a0)
完整路径+=“\\”;
其他的
fullpath+=regfilepath.Substring(a,1);
}
path=fullpath.Substring(0,fullpath.LastIndexOf(“\\”)+1);
}
}
捕获//(例外情况除外)
{
}
返回路径;
}

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

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;
}
string FindPathByInstalledAppEXEName(string appEXEName)
{
字符串路径=string.Empty;
尝试
{
RegistryKey key=Registry.CurrentUser.OpenSubKey(@“Software\Microsoft\Installer\Assembly”);
string regfilepath=string.Empty;
if(key!=null)//确保有程序集
{
foreach(key.GetSubKeyNames()中的字符串Keyname)
{
if(Keyname.IndexOf(appEXEName)>0)
{
regfilepath=Keyname;
打破
}
}
}
如果(!string.IsNullOrEmpty(regfilepath))
{
字符串fullpath=“”;
for(int a=0;a0)
完整路径+=“\\”;
其他的
fullpath+=regfilepath.Substring(a,1);
}
path=fullpath.Substring(0,fullpath.LastIndexOf(“\\”)+1);
}
}
捕获//(例外情况除外)
{
}
返回路径;
}