如何找到EXE的安装位置-正确的方法? [英] How to find an EXE's install location - the proper way?

查看:444
本文介绍了如何找到EXE的安装位置-正确的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#和MATLAB制作一个软件,该软件调用另一个软件(CMG)进行一些处理.我的问题是,我放入程序中的软件的地址仅在我的个人计算机上正确,而在客户的计算机上却不正确(我不知道他们的计算机上使用CMG软件的路径是什么).

I am making a software in C# and MATLAB that calls another software (CMG) to do some processing. My problem is that the address of the software I have put in my program is only correct on my personal computer and not on the customers' computers (I don't know what would be the path to CMG software on their computer).

如何提供一种通用的地址格式,以使其在每台计算机上都能正常工作?

How can I provide a general form of the address in order to make it work on every computer?

以下是我从MATLAB软件调用的路径:

The following is the path I call from my MATLAB software:

C:\Program Files (x86)\CMG\STARS\2011.10\Win_x64\EXE\st201110.exe

如您所见,它位于驱动器C中,版本为2011.10.因此,如果客户版本是其他版本,并且已安装在其他驱动器上,则此路径没有意义.

As you see it is in drive C and the version is 2011.10. So if customer's version is something else and it is installed on other drives, this path makes no sense.

推荐答案

方法1

注册表项 SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall 提供了安装最多个应用程序的列表:

Method 1

The registry keys SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall provides a list of where most applications are installed:

注意:由于某些不需要安装,因此它不会列出PC上的所有EXE应用程序.

Note: It doesn't list all EXE applications on the PC as some dont require installation.

对于您而言,我非常确定CMG STARS将被列出,并且您可以通过遍历 DisplayName 值并获取 InstallLocation .

In your case I am pretty sure that CMG STARS will be listed and you will be able to search for it by iterating over all subkeys looking at the DisplayName value and fetching the InstallLocation.

还请注意,此卸载注册表项存在于注册表中的3个位置:
1.在CurrentUser中安装SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall
2.在LocalMachine内部安装SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall
3. SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \在LocalMachine中卸载

Also note that this Uninstall registry key exists in 3 places in the registry:
1. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall inside CurrentUser
2. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall inside LocalMachine
3. SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall in LocalMachine

这是一个返回应用程序安装位置的类:

Here is an class that returns the installed location of an application:

using Microsoft.Win32;

public static class InstalledApplications
{
    public static string GetApplictionInstallPath(string nameOfAppToFind)
    {
        string installedPath;
        string keyName;

        // search in: CurrentUser
        keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        installedPath = ExistsInSubKey(Registry.CurrentUser, keyName, "DisplayName", nameOfAppToFind);
        if (!string.IsNullOrEmpty(installedPath))
        {
            return installedPath;
        }

        // search in: LocalMachine_32
        keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        installedPath = ExistsInSubKey(Registry.LocalMachine, keyName, "DisplayName", nameOfAppToFind);
        if (!string.IsNullOrEmpty(installedPath))
        {
            return installedPath;
        }

        // search in: LocalMachine_64
        keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
        installedPath = ExistsInSubKey(Registry.LocalMachine, keyName, "DisplayName", nameOfAppToFind);
        if (!string.IsNullOrEmpty(installedPath))
        {
            return installedPath;
        }

        return string.Empty;
    }

    private static string ExistsInSubKey(RegistryKey root, string subKeyName, string attributeName, string nameOfAppToFind)
    {
        RegistryKey subkey;
        string displayName;

        using (RegistryKey key = root.OpenSubKey(subKeyName))
        {
            if (key != null)
            {
                foreach (string kn in key.GetSubKeyNames())
                {
                    using (subkey = key.OpenSubKey(kn))
                    {
                        displayName = subkey.GetValue(attributeName) as string;
                        if (nameOfAppToFind.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true)
                        {
                            return subkey.GetValue("InstallLocation") as string;
                        }
                    }
                }
            }
        }
        return string.Empty;
    }
}

这是您的称呼方式:

string installPath = InstalledApplications.GetApplictionInstallPath(nameOfAppToFind);

要获取nameOfAppToFind,您需要在注册表中的DisplayName处查找:

To get the nameOfAppToFind you'll need to look in the registry at the DisplayName:

REF:我从您还可以使用系统管理.Net DLL来获取 InstallLocation ,尽管它的堆速度较慢,并且会为系统上的每个已安装产品创建"Windows Installer重新配置产品"事件日志消息. /p>

You can also use the System Management .Net DLL to get the InstallLocation although it is heaps slower and creates "Windows Installer reconfigured the product" event log messages for every installed product on your system.

using System.Management;

ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject mo in mos.Get())
{
    Debug.Print(mo["Name"].ToString() + "," + mo["InstallLocation"].ToString() + Environment.NewLine);
}


获取EXE的名称

以上两种方法都不能告诉您可执行文件的名称,但是通过遍历安装路径中的所有文件并使用我讨论的技术


Getting the EXE's name

Neither of the above methods tell you the name of the executable, however it is quite easy to work out by iterating over all the files in the install path and using a technique I discuss here to look at file properties to detect the EXE with the correct File Description, eg:

private string GetFileExeNameByFileDescription(string fileDescriptionToFind, string installPath)
{
    string exeName = string.Empty;
    foreach (string filePath in Directory.GetFiles(installPath, "*.exe"))
    {   
        string fileDescription = GetSpecificFileProperties(filePath, 34).Replace(Environment.NewLine, string.Empty);
        if (fileDescription == fileDescriptionToFind)
        {
            exeName = GetSpecificFileProperties(filePath, 0).Replace(Environment.NewLine, string.Empty);
            break;
        }
    }
    return exeName;
}

无论使用哪种方法(1或2),我建议您保存exe名称的位置,因此仅执行一次此操作.在我看来,最好使用方法1,因为它更快,并且不会创建所有的"Windows Installer重新配置产品".事件日志.

Either method (1 or 2) you use I recommend that you save the location of exe name so you only do this operation once. In my opinion its better to use Method 1 as its faster and doesn't create all the "Windows Installer reconfigured the product." event logs.

如果正在安装应用程序,则可以在安装过程中找出CMG STARS所在的位置

If your application is being installed you could find out where CMG STARS is located during installation Using Windows Installer to Inventory Products and Patches:

枚举产品
使用
MsiEnumProductsEx 函数枚举Windows Installer安装在 系统.此功能可以找到所有每台机器的安装和 针对每个用户的应用程序(托管和非托管)安装 当前用户和系统中的其他用户.使用dwContext 参数以指定要找到的安装上下文.你可以 指定可能安装的任何一种或任何组合 上下文.使用szUserSid参数指定用户上下文 找到应用程序.

Enumerating Products
Use the MsiEnumProductsEx function to enumerate Windows Installer applications that are installed in the system. This function can find all the per-machine installations and per-user installations of applications (managed and unmanaged) for the current user and other users in the system. Use the dwContext parameter to specify the installation context to be found. You can specify any one or any combination of the possible installation contexts. Use the szUserSid parameter to specify the user context of applications to be found.

在安装过程中,您将找到CMG STARS的exe路径,并保存带有该值的注册表项.

During installation you would find the exe path to CMG STARS and save a registry key with the value.

我讨论了如何使用将EXE的安装路径保存在注册表中以用于在此处更新应用程序.

如评论中所述,值得在注册表中搜索EXE的名称​​ st201110.exe ,并查看CMG STAR应用程序的作者是否已在注册表中提供了此信息.您可以直接访问的密钥.

As mentioned in the comments, it is worthwhile you do a search in the registry for the EXE's name st201110.exe and see if the authors of the CMG STAR application already provide this information in a registry key you can access directly.

如果所有其他操作均失败,则向用户显示FileOpenDialog,并让他们手动指定exe的路径.

If all else fails present the user with a FileOpenDialog and get them to specify the exe's path manually.

我提到将安装路径和exe名称存储在注册表(或数据库,配置文件等)中,并且在对其进行任何外部调用之前,应始终检查exe文件是否存在,例如:

I mentioned to store the install path and exe name in the registry (or database, config file, etc) and you should always check the exe file exists before making any external calls to it, eg:

if (!File.Exists(installPath + exeName))
{
//Run through the process to establish where the 3rd party application is installed
}

这篇关于如何找到EXE的安装位置-正确的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆