visual-studio 以编程方式检索 Visual Studio 安装目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30504/
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
Programmatically retrieve Visual Studio install directory
提问by Emperor XLII
I know there is a registry key indicating the install directory, but I don't remember what it is off-hand.
我知道有一个注册表项指示安装目录,但我不记得它是什么。
I am currently interested in Visual Studio 2008 install directory, though it wouldn't hurt to list others for future reference.
我目前对 Visual Studio 2008 安装目录感兴趣,尽管列出其他目录以供将来参考也无妨。
采纳答案by ZebZiggle
I'm sure there's a registry entry as well but I couldn't easily locate it. There is the VS90COMNTOOLS environment variable that you could use as well.
我确定也有一个注册表项,但我无法轻松找到它。您也可以使用 VS90COMNTOOLS 环境变量。
回答by Dim_Ka
I use this method to find the installation path of Visual Studio 2010:
我用这个方法找到Visual Studio 2010的安装路径:
private string GetVisualStudioInstallationPath()
{
string installationPath = null;
if (Environment.Is64BitOperatingSystem)
{
installationPath = (string)Registry.GetValue(
"HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\",
"InstallDir",
null);
}
else
{
installationPath = (string)Registry.GetValue(
"HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\VisualStudio\10.0\",
"InstallDir",
null);
}
return installationPath;
}
回答by Kevin Kibler
Registry Method
注册方式
I recommend querying the registry for this information. This gives the actual installation directory without the need for combining paths, and it works for express editions as well. This could be an important distinction depending on what you need to do (e.g. templates get installed to different directories depending on the edition of Visual Studio). The registry locations are as follows (note that Visual Studio is a 32-bit program and will be installed to the 32-bit section of the registry on x64 machines):
我建议查询注册表以获取此信息。这给出了实际的安装目录,而无需组合路径,它也适用于 express 版本。这可能是一个重要的区别,具体取决于您需要做什么(例如,根据 Visual Studio 的版本将模板安装到不同的目录)。注册表位置如下(注意 Visual Studio 是 32 位程序,将安装到 x64 机器上的注册表的 32 位部分):
- Visual Studio: HKLM\SOFTWARE\Microsoft\Visual Studio\Major.Minor:InstallDir
- Visual C# Express: HKLM\SOFTWARE\Microsoft\VCSExpress\Major.Minor:InstallDir
- Visual Basic Express: HKLM\SOFTWARE\Microsoft\VBExpress\Major.Minor:InstallDir
- Visual C++ Express: HKLM\SOFTWARE\Microsoft\VCExpress\Major.Minor:InstallDir
- Visual Studio: HKLM\SOFTWARE\Microsoft\Visual Studio\Major.Minor:InstallDir
- Visual C# Express:HKLM\SOFTWARE\Microsoft\VCSExpress\Major.Minor:InstallDir
- Visual Basic Express:HKLM\SOFTWARE\Microsoft\VBExpress\Major.Minor:InstallDir
- Visual C++ Express:HKLM\SOFTWARE\Microsoft\VCExpress\Major.Minor:InstallDir
where Major is the major version number, Minor is the minor version number, and the text after the colon is the name of the registry value. For example, the installation directory of Visual Studio 2008 Professional would be located at the HKLM\SOFTWARE\Microsoft\Visual Studio\9.0key, in the InstallDirvalue.
其中 Major 是主要版本号,Minor 是次要版本号,冒号后面的文本是注册表值的名称。例如,Visual Studio 2008 Professional 的安装目录将位于HKLM\SOFTWARE\Microsoft\Visual Studio\9.0键的InstallDir值中。
Here's a code example that prints the installation directory of several versions of Visual Studio and Visual C# Express:
下面的代码示例打印了多个版本的 Visual Studio 和 Visual C# Express 的安装目录:
string visualStudioRegistryKeyPath = @"SOFTWARE\Microsoft\VisualStudio";
string visualCSharpExpressRegistryKeyPath = @"SOFTWARE\Microsoft\VCSExpress";
List<Version> vsVersions = new List<Version>() { new Version("10.0"), new Version("9.0"), new Version("8.0") };
foreach (var version in vsVersions)
{
foreach (var isExpress in new bool[] { false, true })
{
RegistryKey registryBase32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
RegistryKey vsVersionRegistryKey = registryBase32.OpenSubKey(
string.Format(@"{0}\{1}.{2}", (isExpress) ? visualCSharpExpressRegistryKeyPath : visualStudioRegistryKeyPath, version.Major, version.Minor));
if (vsVersionRegistryKey == null) { continue; }
Console.WriteLine(vsVersionRegistryKey.GetValue("InstallDir", string.Empty).ToString());
}
Environment Variable Method
环境变量法
The non-express editions of Visual Studio also write an environment variable that you could check, but it gives the location of the common tools directory, not the installation directory, so you'll have to do some path combining. The format of the environment variable is VS*COMNTOOLSwhere * is the major and minor version number. For example, the environment variable for Visual Studio 2010 is VS100COMNTOOLSand contains a value like C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools.
Visual Studio 的非express 版本也写了一个环境变量,你可以检查,但它给出的是通用工具目录的位置,而不是安装目录,所以你必须做一些路径组合。环境变量的格式为VS*COMNTOOLS,其中 * 是主要和次要版本号。例如,Visual Studio 2010 的环境变量是VS100COMNTOOLS并且包含类似C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools 的值。
Here's some example code to print the environment variable for several versions of Visual Studio:
下面是一些示例代码,用于打印多个版本的 Visual Studio 的环境变量:
List<Version> vsVersions = new List<Version>() { new Version("10.0"), new Version("9.0"), new Version("8.0") };
foreach (var version in vsVersions)
{
Console.WriteLine(Path.Combine(Environment.GetEnvironmentVariable(string.Format("VS{0}{1}COMNTOOLS", version.Major, version.Minor)), @"..\IDE"));
}
回答by Emperor XLII
Environment:Thanks to Zeb and Sam for the VS*COMNTOOLSenvironment variable suggestion. To get to the IDE in PowerShell:
环境:感谢 Zeb 和 Sam 提供VS*COMNTOOLS环境变量建议。要在 PowerShell 中访问 IDE:
$vs = Join-Path $env:VS90COMNTOOLS '..\IDE\devenv.exe'
Registry:Looks like the registry location is HKLM\Software\Microsoft\VisualStudio, with version-specific subkeys for each install. In PowerShell:
注册表:看起来注册表位置是HKLM\Software\Microsoft\VisualStudio,每个安装都有特定于版本的子项。在 PowerShell 中:
$vsRegPath = 'HKLM:\Software\Microsoft\VisualStudio.0'
$vs = (Get-ItemProperty $vsRegPath).InstallDir + 'devenv.exe'
[Adapted from here]
[改编自这里]
回答by vik_78
For Visual Studio 2017 and Visual Studio 2019 there is the Setup API from Microsoft.
对于 Visual Studio 2017 和 Visual Studio 2019,有来自 Microsoft 的 Setup API。
In C#, just add the NuGet package "Microsoft.VisualStudio.Setup.Configuration.Interop", and use it in this way:
在C#中,只需添加NuGet包“Microsoft.VisualStudio.Setup.Configuration.Interop”,这样使用即可:
try {
var query = new SetupConfiguration();
var query2 = (ISetupConfiguration2)query;
var e = query2.EnumAllInstances();
var helper = (ISetupHelper)query;
int fetched;
var instances = new ISetupInstance[1];
do {
e.Next(1, instances, out fetched);
if (fetched > 0)
Console.WriteLine(instances[0].GetInstallationPath());
}
while (fetched > 0);
return 0;
}
catch (COMException ex) when (ex.HResult == REGDB_E_CLASSNOTREG) {
Console.WriteLine("The query API is not registered. Assuming no instances are installed.");
return 0;
}
You can find more samples for VC, C#, and VB here.
您可以在此处找到更多 VC、C# 和 VB 示例。
回答by HanT
It is a real problem that all Visual Studio versions have their own location. So the solutions here proposed are not generic. However, Microsoft has made a utility available for free (including the source code) that solved this problem (i.e. annoyance). It is called vswhere.exeand you can download it from here. I am very happy with it, and hopefully it will also do for future releases. It makes the whole discussion on this page redundant.
所有 Visual Studio 版本都有自己的位置,这是一个真正的问题。所以这里提出的解决方案不是通用的。然而,微软已经免费提供了一个实用程序(包括源代码)来解决这个问题(即烦恼)。它被调用vswhere.exe,您可以从这里下载。我对它非常满意,希望它也适用于未来的版本。它使此页面上的整个讨论变得多余。
回答by JJS
@Dim-Ka has a great answer. If you were curious how you'd implement this in a batch script, this is how.
@Dim-Ka 有一个很好的答案。如果您对如何在批处理脚本中实现这一点感到好奇,这就是方法。
@echo off
:: BATCH doesn't have logical or, otherwise I'd use it
SET platform=
IF /I [%PROCESSOR_ARCHITECTURE%]==[amd64] set platform=true
IF /I [%PROCESSOR_ARCHITEW6432%]==[amd64] set platform=true
:: default to VS2012 = 11.0
:: the Environment variable VisualStudioVersion is set by devenv.exe
:: if this batch is a child of devenv.exe external tools, we know which version to look at
if not defined VisualStudioVersion SET VisualStudioVersion=11.0
if defined platform (
set VSREGKEY=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\%VisualStudioVersion%
) ELSE (
set VSREGKEY=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\%VisualStudioVersion%
)
for /f "skip=2 tokens=2,*" %%A in ('reg query "%VSREGKEY%" /v InstallDir') do SET VSINSTALLDIR=%%B
echo %VSINSTALLDIR%
回答by JJS
Ah, the 64-bit machine part was the issue. It turns out I need to make sure I'm running the PowerShell.exe under the syswow64 directory in order to get the x86 registry keys.
啊,64 位机器部分是问题所在。事实证明,我需要确保我正在 syswow64 目录下运行 PowerShell.exe 才能获取 x86 注册表项。
Now that wasn't very fun.
现在那不是很有趣。
回答by peter
Use Environment.GetEnvironmentVariable("VS90COMNTOOLS");.
使用Environment.GetEnvironmentVariable("VS90COMNTOOLS");.
Also in a 64-bit environment, it works for me.
同样在 64 位环境中,它对我有用。
回答by Stefan Cepcik
Here's a solution to always get the path for the latest version:
这是始终获取最新版本路径的解决方案:
$vsEnvVars = (dir Env:).Name -match "VS[0-9]{1,3}COMNTOOLS"
$latestVs = $vsEnvVars | Sort-Object | Select -Last 1
$vsPath = Get-Content Env:$latestVs

