以编程方式查找VS2017安装目录

时间:2016-12-12 17:37:43

标签: visual-studio visual-studio-2017

使用以前版本的VS,您可以查询注册表以确定VS的安装目录:

  

HKLM \ SOFTWARE \ Wow6432Node \微软\ VisualStudio的\ 14.0

然而,这似乎不适用于VS2017 RC。我们有脚本可以检测最新安装的VS,然后执行“正确的操作”,到目前为止,我遇到了将VS2017插入这些系统的问题。

有谁知道如何以编程方式确定VS2017的安装位置?

10 个答案:

答案 0 :(得分:12)

您可以使用vswhere工具获取VS2017位置。

示例:

@echo off

rem VS2017U2 contains vswhere.exe
if "%VSWHERE%"=="" set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"

for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" (
  "%InstallDir%\MSBuild\15.0\Bin\MSBuild.exe" %*
)

您可以在此处详细了解:https://blogs.msdn.microsoft.com/heaths/2017/02/25/vswhere-available/

答案 1 :(得分:5)

Visual Studio 2017支持所有SKU(企业版,专业版和社区版)的无注册表,并排安装。

MSI安装程序可以通过此处描述的API进行查询: https://blogs.msdn.microsoft.com/heaths/2016/09/15/changes-to-visual-studio-15-setup/

示例如下:

答案 2 :(得分:1)

由于批量延迟扩展" KindDragon的解决方案对我来说并不适合。 "特征&#34 ;. (WAT)

这是我的代码,兼容VS 2017 15.2(用于vswhere.exe安装)

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.Component.MSBuild -property installationPath`) do (
  set InstallDir=%%i
)

if exist "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat" (
  call "!InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
) else (
  echo "Could not find !InstallDir!\VC\Auxiliary\Build\vcvars64.bat"
)

特别注意使用SETLOCAL EnableDelayedExpansion和!InstallDir!

答案 3 :(得分:1)

嗯, vswhere.exe 实际上并不提供Visual Studio版本安装路径。这是我的.profile文件,来自2008年的Interix片段做了同样的小更新(shell脚本):

if [[ -n $PROCESSOR_ARCHITEW6432 || $PROCESSOR_ARCHITECTURE != "x86" ]]; then
  hkeybase='HKLM\SOFTWARE\Wow6432Node\Microsoft\'
else
  hkeybase='HKLM\SOFTWARE\Microsoft\'
fi
for vsver in "15.0" "14.0" "12.0" "11.0" "10.0" "9.0" "8.0"; do
  _vsinstalldir=$(reg.exe query ${hkeybase}'VisualStudio\SxS\VS7' -v $vsver 2>/dev/null \
   | sed -n 's|.*REG_SZ *\([ [:print:]]*\).*|\1|p' | sed 's|\\|/|g')
if [[ -n $_vsinstalldir ]]; then break; fi
done; unset vsver

这是枚举Visual Studio安装,支持最新的注册表项

HKLM\SOFTWARE\Microsoft\VisualStudio\SxS\VS7

仍在为Visual Studio 2017工作。很容易转换为cmd语法。要查询注册表更简单,并且不需要路径中的vswhere.exe,因此有利的IMO。

现在找到当前的Visual C ++实例和SDK完全是另一项任务。 :d

您想知道的常见输出:

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/

答案 4 :(得分:1)

我花了很多时间试图修改Srekel的答案以仅搜索VS2017。注意:如果将“ for”语句放在“ if”块内,将破坏转义符,将不起作用。

SETLOCAL EnableDelayedExpansion

if not exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
  echo "WARNING: You need VS 2017 version 15.2 or later (for vswhere.exe)"
)

set vswherestr=^"!ProgramFiles(x86)!\Microsoft Visual Studio\Installer\vswhere.exe^" -version [15.0,16.0^^) -products * -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`!vswherestr!`) do (  
  set BUILDVCTOOLS=%%i\Common7\Tools
  echo BUILDVCTOOLS: !BUILDVCTOOLS!
  if not exist !BUILDVCTOOLS!\VsDevCmd.bat (
    echo Error: Cannot find VS2017 Build Tools
    goto :buildfailed
  )
  call "!BUILDVCTOOLS!\VsDevCmd.bat"
)    

答案 5 :(得分:0)

我可以推荐我的软件包get-vs2017-path它只使用内置的Windows工具(虽然它是作为NPM软件包构建的,但它没有依赖关系,而且tools文件夹独立工作)

答案 6 :(得分:0)

在此处查看以下答案:

https://stackoverflow.com/a/55754831/2338477

您可以使用任一命令行工具来查询Visual Studio位置,但是我还提供了编程方式来查询Visual Studio位置。代码基于import os import shutil path = r"C:\Users\vasudeos\OneDrive\Desktop\newfolder" for x in os.listdir(path): file = os.path.join(path, x) if not os.path.isdir(file): folder_name = os.path.join(path, os.path.splitext(x)[0]) if not os.path.exists(folder_name): os.mkdir(folder_name) shutil.move(file, folder_name) 源代码,但已简化。

答案 7 :(得分:0)

我们只有3个Visual Studio版本。

  • \社区
  • \ Professional
  • \ Enterprise

这样我们就可以简化所有内容。
这里是一些经过测试的CMD脚本。

@SET env_all_vs2017_root=%ProgramFiles(x86)%\Microsoft Visual Studio\2017
@SET env_vs2017_path="%env_all_vs2017_root%\Professional"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Community"
@IF NOT EXIST %env_vs2017_path%  SET env_vs2017_path="%env_all_vs2017_root%\Enterprise"
@REM Let's fail laudly
@IF NOT EXIST %env_vs2017_path%  SET "env_vs2017_path=Visual Studio 2017 install path was not found by %~nx0"

@REM  You may want to remove quotes
@SET unquoted=%env_vs2017_path:"=%


@REM  And now let's see the result and PAUSE

@ECHO VS 2017 install path is
@ECHO %unquoted%

@PAUSE

答案 8 :(得分:-2)

您可以使用此PowerShell代码段查找VS2017安装目录:

$vssetup_path = "$([Environment]::GetFolderPath("MyDocuments"))\WindowsPowerShell\Modules\VSSetup"
if (-not (Test-Path $vssetup_path -pathType container))
{
    iwr https://github.com/Microsoft/vssetup.powershell/releases/download/1.0.36-rc/VSSetup.zip -OutFile "$env:TEMP\VSSetup.zip"
    Expand-Archive "$env:TEMP\VSSetup.zip" $vssetup_path
}
$vs2017 = Get-VSSetupInstance -All | Select-VSSetupInstance -Require 'Microsoft.VisualStudio.Workload.NativeDesktop' -Version '[15.0,16.0)' -Latest
"Installation Path: " + $vs2017.InstallationPath
#`vsdevcmd.bat -arch=x86` or `vsdevcmd.bat -arch=amd64` can be used to setup path's to VC++ compiler
"VsDevCmd.bat Path: " + $vs2017.InstallationPath + "\Common7\Tools\VsDevCmd.bat"

答案 9 :(得分:-3)

我使用像KindDragon建议的powershell

$Is64bitOs = $env:PROCESSOR_ARCHITEW6432 -eq 'AMD64';
if ($Is64bitOs){
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0";
}
else {
    $registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0";
}

$VSInstallDir = (Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\14.0 -Name ShellFolder).ShellFolder;