查找是否安装第三方软件,安装路径和EXE的名字? [英] Find if 3rd party software is installed, the install path and the EXE's name?

查看:341
本文介绍了查找是否安装第三方软件,安装路径和EXE的名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在C#和MATLAB软件调用其他软件(CMG)做一些处理。我的问题是,我已经把我的程序软件的地址,只是我的个人电脑上,而不是在客户的电脑(我不知道什么是他们的计算机上的路径CMG软件)是正确的。



我怎样才能提供地址的一般形式,以使其在每台计算机上运行?



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

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

正如你看到它是在驱动器C版本为2011.10。所以,如果客户的版本是别的东西,它是安装在其他驱动器,这条道路是没有意义的。


解决方案

方法1



该注册表项的 SOFTWARE\\ \\Microsoft\Windows\CurrentVersion\Uninstall 提供了一个列表,其中的的应用程序安装:





请注意:它不会列出所有PC为一些对EXE应用程序不要求安装。



在你的情况我敢肯定,CMG STARS将陆续上市,你将能够通过迭代寻找它在看的显示名称价值和获取在 INSTALLLOCATION



的所有子项

另外请注意,此卸载注册表项存在在注册表中的3个地方:结果
1的currentUser结果
2. SOFTWARE\Microsoft\Windows\CurrentVersion内SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall \Uninstall内LOCALMACHINE结果
3. SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall在LOCALMACHINE



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

 使用的Microsoft.Win32; 

公共静态类InstalledApplications
{
公共静态字符串GetApplictionInstallPath(字符串nameOfAppToFind)
{
串installedPath;
串的keyName;

//搜索在:的currentUser
的keyName = @SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
installedPath = ExistsInSubKey(Registry.CurrentUser,注册表,显示名称,nameOfAppToFind);
如果
{
返回installedPath(string.IsNullOrEmpty(installedPath)!);
}

//搜索在:LocalMachine_32
的keyName = @SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
installedPath = ExistsInSubKey(Registry.LocalMachine,注册表,显示名称,nameOfAppToFind);
如果
{
返回installedPath(string.IsNullOrEmpty(installedPath)!);
}

//搜索在:LocalMachine_64
的keyName = @SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
installedPath = ExistsInSubKey(Registry.LocalMachine,注册表,显示名称,nameOfAppToFind);
如果
{
返回installedPath(string.IsNullOrEmpty(installedPath)!);
}

返回的String.Empty;
}

私人静态字符串ExistsInSubKey(根的RegistryKey,串subKeyName,字符串的attributeName,串nameOfAppToFind)
{
的RegistryKey子项;
字符串显示名;使用

(的RegistryKey键= root.OpenSubKey(subKeyName))
{
如果(关键!= NULL)
{
的foreach(字符串在KN key.GetSubKeyNames())
{
使用(子= key.OpenSubKey(KN))
{
=的displayName subkey.GetValue(的attributeName)的字符串;
如果(nameOfAppToFind.Equals(显示名,StringComparison.OrdinalIgnoreCase)==真)
{
返回subkey.GetValue(INSTALLLOCATION)作为字符串;
}
}
}
}
}
返回的String.Empty;
}
}



REF:我修改这里从上面的代码返回安装路径






方法2



您还可以使用系统管理NET的DLL来获得的 INSTALLLOCATION 虽然是慢堆,并创建Windows安装程序重新配置的产品为您的系统上安装的每个产品的事件日志消息。

 使用System.Management; 

ManagementObjectSearcher MOS =新ManagementObjectSearcher(SELECT * FROM的Win32_Product);
的foreach(MO的ManagementObject在mos.Get())
{
Debug.Print(MO [名称]。的ToString()+,+莫[INSTALLLOCATION。的ToString()+ Environment.NewLine);
}






获取EXE文件的名称



无论上述方法告诉你的可执行文件的名称,但它是很容易通过遍历所有文件的安装路径,并使用技术,我摸出讨论 这里看看文件属性 检测EXE用正确的文件说明,例如:

 私人字符串GetFileExeNameByFileDescription(串fileDescriptionToFind,串INSTALLPATH)
{
串EXENAME =的String.Empty;
的foreach(在Directory.GetFiles字符串文件路径(安装路径,一个* .exe))
{
串fileDescription = GetSpecificFileProperties(文件路径,34).Replace(Environment.NewLine,的String.Empty );
如果(fileDescription == fileDescriptionToFind)
{
EXENAME = GetSpecificFileProperties(文件路径,0).Replace(Environment.NewLine,的String.Empty);
中断;
}
}
返回EXENAME;
}








这两种方法(1或2)你用我建议您保存exe文件名的位置,所以你只能做此操作一次。在我看来,它能够更好地使用方法1作为它的速度更快,不会创建所有的Windows安装程序重新配置的产品。使用安装程序

如果事件日志。






备用方法您的应用程序被安装,你可以找出CMG STARS期间安装位于使用Windows安装到库存产品和修补




枚举产品结果
使用 MsiEnumProductsEx 函数来枚举安装在Windows Installer应用程序
系统。此功能可以找到所有的每个机器的安装和

当前用户和其他用户在系统中的应用程序(托管和非托管)每用户安装。使用dwContext
参数指定安装的上下文中找到。可以
指定的任何一个或可能的安装
上下文的任何组合。使用szUserSid参数指定要发现
应用程序的用户上下文。




在安装过程中,你会发现exe文件路径为CMG STARS和保存价值的注册表项。



我讨论使用的在注册表中保存一个EXE的安装路径这里更新应用程序。






提示



作为评价所提到的,它是值得你这样做在注册表的EXE的名字搜索的 st201110.exe 的,看看CMG STAR应用程序的作者已经提供一个注册表项此信息,您可以直接访问。






b计划



如果一切都失败了FileOpenDialog目前的用户即可获得他们手动指定exe文件的路径。






如果第三方应用被卸载或升级?



我提到存储安装在注册表路径和exe文件名(或数据库,配置文件等),您应经常检查作出任何外部调用它,比如之前的exe文件存在(!File.Exists(INSTALLPATH + EXENAME))

 如果
{
//通过运行这个过程建立在那里的第三方应用程序安装
}


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?

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

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

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.

解决方案

Method 1

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

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

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.

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

REF: I modified the above code from here to return the install path.


Method 2

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


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


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.


Alternate Method using an Installer

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:

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.

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

I discuss using this approach of saving an EXE's install path in the registry for updating applications here.


Tip

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.


Plan B

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


What if the 3rd party application is uninstalled or upgraded?

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天全站免登陆