我知道有一个注册表项可以指示安装目录,但我现在无法立刻回忆起它的名称。
我目前对 Visual Studio 2008 的安装目录感兴趣,但也可以列出其他的以供日后参考。
我知道有一个注册表项可以指示安装目录,但我现在无法立刻回忆起它的名称。
我目前对 Visual Studio 2008 的安装目录感兴趣,但也可以列出其他的以供日后参考。
我使用这个方法来查找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;
}
建议查询注册表以获取此信息。这可以获得实际的安装目录,无需组合路径,并且适用于Express版本。这可能是一个重要的区别,具体取决于您需要做什么(例如,根据Visual Studio的版本,模板会安装到不同的目录)。注册表位置如下(请注意,Visual Studio是32位程序,在x64机器上将安装到注册表的32位部分):
其中Major是主要版本号,Minor是次要版本号,冒号后面的文本是注册表值的名称。例如,Visual Studio 2008 Professional的安装目录位于HKLM\SOFTWARE\Microsoft\Visual Studio\9.0键中的InstallDir值。
以下是打印多个版本的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());
}
非 Express 版本的 Visual Studio 也会写入一个环境变量,您可以检查该变量,但它给出的是常见工具目录的位置,而不是安装目录,因此您需要进行一些路径组合操作。环境变量的格式为 VS*COMNTOOLS,其中 * 是主要和次要版本号。例如,Visual Studio 2010 的环境变量为 VS100COMNTOOLS,包含类似于 C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools 的值。
下面是一些示例代码,用于打印几个版本的 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"));
}
VSxxxCOMNTOOLS
变量。我想知道是否有另一种方法而不需要注册表。 - Royi我相信也有一个注册表项,但我无法轻松地找到它。您还可以使用VS90COMNTOOLS环境变量。
$vs = Join-Path $env:VS90COMNTOOLS '..\IDE\devenv.exe'
注册表: 看起来注册表位置是 HKLM\Software\Microsoft\VisualStudio
,每个安装都有特定版本的子键。在PowerShell中:
$vsRegPath = 'HKLM:\Software\Microsoft\VisualStudio\9.0'
$vs = (Get-ItemProperty $vsRegPath).InstallDir + 'devenv.exe'
[Adapted from here]
对于 Visual Studio 2017 和 Visual Studio 2019,微软提供了设置 API。
在 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;
}
所有的 Visual Studio 版本各自独立存在的问题确实很麻烦。因此,这里提出的解决方案并不通用。但是,微软已经免费提供了一个可用于解决此问题(即烦恼)的工具(包括源代码)。它被称为vswhere.exe
,您可以从这里下载。我非常喜欢它,希望它也可以适用于未来的版本。这使得本页上的整个讨论都变得无意义。
以下是一种始终获取最新版本路径的解决方案:
$vsEnvVars = (dir Env:).Name -match "VS[0-9]{1,3}COMNTOOLS"
$latestVs = $vsEnvVars | Sort-Object | Select -Last 1
$vsPath = Get-Content Env:\$latestVs
@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%
使用Environment.GetEnvironmentVariable("VS90COMNTOOLS");
。
在64位环境下,我也可以正常工作。
啊,原来问题出在64位机器上。事实证明,为了获取x86注册表键,我需要确保在syswow64目录下运行PowerShell.exe。
现在这可不是什么有趣的事情。