C# 如何获取应用程序的安装路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/909788/
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 to get installation path of an application?
提问by SyncMaster
In Windows using C#, how can I get the installation path of a software (for example consider NUnit or any other software like MS word, etc.) from my project? Also how to set the path variables that we set in Environment variables so that we can run the application just by giving in command prompt.
在使用 C# 的 Windows 中,如何从我的项目中获取软件的安装路径(例如考虑 NUnit 或任何其他软件,如 MS word 等)?还有如何设置我们在环境变量中设置的路径变量,以便我们只需在命令提示符中给出即可运行应用程序。
Like if I install NUnit in "C:\Program Files" I can run it by giving 'NUnit' in cmd prompt but if I install in a different location I can't do the same. I need to get the location or path of NUnit or any other software installed in my system (having Windows XP) from my project.
就像我在“C:\ 我需要从我的项目中获取 NUnit 或安装在我的系统(使用 Windows XP)中的任何其他软件的位置或路径。
EDIT: Like I can get the path of installed program from registry. HKEY_CURRENT_USER->SOFTWARE
编辑:就像我可以从注册表中获取已安装程序的路径一样。HKEY_CURRENT_USER->软件
采纳答案by Johan Bresler
Use the system and application classes. This will give you all sorts of information.
使用系统和应用程序类。这将为您提供各种信息。
EG: Application.ExecutablePath
EG:Application.ExecutablePath
It also provides methods to do what you want to.
它还提供了做你想做的事情的方法。
Edit: Also see registry read/write instructions here:
编辑:另请参阅此处的注册表读/写说明:
回答by Priyank Bolia
Like if i install Nunit in "C:\Program Files" i can run it by giving 'nunit' in cmd prompt but if i install in a different location i cant do the same.
就像我在“C:\
May be you are using Windows Vista, which can search in Program Files, but won't look in other folders.
可能您使用的是 Windows Vista,它可以在 Program Files 中搜索,但不会在其他文件夹中查找。
In windows using C#, how to get the installation path of a software(for example consider nunit).?
在windows下使用C#,如何获取某个软件的安装路径(比如nunit)?
It depends, how you are installing the application. The installer knows the path, you may program the installer to write that path to somewhere, say registry.
这取决于您如何安装应用程序。安装程序知道路径,您可以编程安装程序将该路径写入某个地方,例如注册表。
Also how to set the path variables that we set in Environment variables so that we can run the application just by giving in command prompt.
还有如何设置我们在环境变量中设置的路径变量,以便我们只需在命令提示符中给出即可运行应用程序。
回答by VVS
string appFileName = Environment.GetCommandLineArgs()[0];
will give you the full path of the executable and
将为您提供可执行文件的完整路径和
string directory = Path.GetDirectoryName(appFileName);
extracts the directory.
提取目录。
string envPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable(envPath + ";" + yourPath);
edits the PATH environment variable for the current process.
编辑当前进程的 PATH 环境变量。
回答by kedar kamthe
Application.StartupPath is used to get installation location in c#.
Application.StartupPath 用于获取 c# 中的安装位置。
回答by kedar kamthe
Application.ExecutablePath (includes filename)
Application.StartupPath (not includes filename)
This will give you the path where the application started. Hopefully it will be the installation path.
这将为您提供应用程序启动的路径。希望它是安装路径。
回答by CDJ
Steps to extract value from registry are shown in following code snippet. You may already know that there are no standard rules for applications to place their installation info. The steps shown below are for COM based applications where the appplication must provide Local executable path in a reasonably standard manner.
从注册表中提取值的步骤显示在以下代码片段中。您可能已经知道应用程序没有放置安装信息的标准规则。下面显示的步骤适用于基于 COM 的应用程序,其中应用程序必须以合理的标准方式提供本地可执行路径。
For non-com applications, check to see if some data can be extracted from installed applications cache.
对于非 com 应用程序,请检查是否可以从已安装的应用程序缓存中提取某些数据。
I hate to admit that the solution is not as elegant as I want it to be. Each subkey has to opened in series and opening in single method does not work.
我不愿承认该解决方案并不像我希望的那样优雅。每个子项都必须连续打开,并且在单一方法中打开不起作用。
//string hiveName = @"CLSID"; // for 64 bit COM 7applications
string hiveName = @"WOW6432Node\CLSID"; // for 32 bit COM applications
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(hiveName))
{
if (key != null) {
using (RegistryKey key2 = key.OpenSubKey("{<YourAppGUID>}"))
{
if (key2 != null) {
using (RegistryKey key3 = key2.OpenSubKey("LocalServer32"))
{
if (key3 != null) {
return key3.GetValue("").ToString();
}
}