windows 如何以编程方式获取另一个应用程序的安装路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3916713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get another application's installation path programmatically?
提问by Andrei
I'd like to know where the installation path for an application is. I know it usually is in ...\Program Files... but I guess some people install it in different locations. I do know the name of the application.
我想知道应用程序的安装路径在哪里。我知道它通常在 ...\Program Files ... 中,但我猜有些人将它安装在不同的位置。我知道应用程序的名称。
Thank you.
谢谢你。
回答by Alex McBride
The ideal way to find a program's installation path (on Windows) is to read it from the registry. Most installers will create a registry key for that program that contains the installation path. Exactly where this key is and what it will be named varies depending on the program in question.
查找程序安装路径(在 Windows 上)的理想方法是从注册表中读取它。大多数安装程序会为该程序创建一个包含安装路径的注册表项。此密钥的确切位置以及它将命名的内容因相关程序而异。
To find if the program has a key in the registry, open 'regedit' and use the Edit > Find option to try and locate a key with the program name. If such a key exists, you can read it using the RegistryKeyclass in the .NET Framework library.
要查找该程序在注册表中是否有一个键,请打开“ regedit”并使用“编辑”>“查找”选项尝试查找具有该程序名称的键。如果存在这样的键,您可以使用.NET Framework 库中的RegistryKey类读取它。
If the program does not have a registry key then another option is just to ask the user to locate the .exe file with the OpenFileDialog, although this is obviously not ideal.
如果程序没有注册表项,那么另一个选择就是要求用户使用 OpenFileDialog 定位 .exe 文件,尽管这显然不理想。
回答by Andreas Rejbrand
Many (most?) programs create an App Paths
registry key. Have a look at
许多(大多数?)程序会创建一个App Paths
注册表项。看一下
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
回答by Gern Blanston
If you know the application in question (as compared to any application) registry key is the probably the best option (if one exists).
如果您知道有问题的应用程序(与任何应用程序相比),则注册表项可能是最佳选择(如果存在)。
The install might put in its own custom "install path key" somewhere (so do a find as Fara mentioned) or it might be in the uninstall section for installed programs, so you could check:
安装可能会在某处放置自己的自定义“安装路径键”(如 Fara 提到的那样进行查找),或者它可能位于已安装程序的卸载部分,因此您可以检查:
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
But be aware that any new version of an install could change the key it writes out, both for a custom key or for the uninstall entry. So checking the registry should probably be only for a known install\version.
但请注意,安装的任何新版本都可能更改它写出的密钥,无论是自定义密钥还是卸载条目。因此,检查注册表可能只针对已知的安装\版本。
tep
温度
回答by Sandeep Singh Rawat
Best way is to use Installer APIs to find the program location. You can write a Managed wrapper over the APIs
最好的方法是使用安装程序 API 来查找程序位置。您可以在 API 上编写托管包装器
Search for MsiGetProductInfo
搜索 MsiGetProductInfo
Reference: http://msdn.microsoft.com/en-us/library/aa369558(VS.85).aspx
参考:http: //msdn.microsoft.com/en-us/library/aa369558(VS.85).aspx
回答by Ali Alavi
You can use MSI (I wrote a C# wrapper for it here https://github.com/alialavia/MSINet). Here is a simple example:
您可以使用 MSI(我在这里为它编写了一个 C# 包装器https://github.com/alialavia/MSINet)。这是一个简单的例子:
var location = "";
foreach (var p in InstalledProduct.Enumerate())
{
try
{
if (p.InstalledProductName.Contains("AppName"))
{
location = p.InstallLocation;
break;
}
}
catch { }
}
回答by colmde
Take a look in the registry.
在注册表中查看一下。
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\
or
或者
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
Each of the above contain a list of sub-keys, one for each installed application (as it appears, for example, in the "Programs and Features" applet)
以上每个都包含一个子键列表,每个安装的应用程序都有一个子键(例如,它出现在“程序和功能”小程序中)
You can search for your application there, or if you know the product code, access it directly.
您可以在那里搜索您的应用程序,或者如果您知道产品代码,则直接访问它。
public string GetInstallPath(string applicationName)
{
var installPath = FindApplicationPath(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", applicationName);
if (installPath == null)
{
installPath = FindApplicationPath(@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall", applicationName);
}
return installPath;
}
private string FindApplicationPath(string keyPath, string applicationName)
{
var hklm = Registry.LocalMachine;
var uninstall = hklm.OpenSubKey(keyPath);
foreach (var productSubKey in uninstall.GetSubKeyNames())
{
var product = uninstall.OpenSubKey(productSubKey);
var displayName = product.GetValue("DisplayName");
if (displayName != null && displayName.ToString() == applicationName)
{
return product.GetValue("InstallLocation").ToString();
}
}
return null;
}