Visual Studio 2017 命令行默认目录

自从安装了Visual Studio 2017 后,在使用时有一个最大的不同一直困扰着我,今天终于下决心花了十来分钟解决了下。
我习惯于使用快捷方式或者在资源管理器中使用StEx加上一个vs编译命令行,而之前的VS版本命令行打开后都是在当前目录Visual Studio 2017这个奇葩打开后一直是cd到我的C:\Users\UserName\source下。
由于我启动命令行使用的命令是%comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
经过递归查找vcvarsall.bat里包含所有批处理文件,最后在C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\vsdevcmd\core\vsdevcmd_end.bat中找到了以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
@REM Set the current directory that users will be set after the script completes
@REM in the following order:
@REM 1. [VSCMD_START_DIR] will be used if specified in the user environment
@REM 2. [USERPROFILE]\source if it exists
@REM 3. current directory
if "%VSCMD_START_DIR%" NEQ "" (
cd /d "%VSCMD_START_DIR%"
) else (
if EXIST "%USERPROFILE%\Source" (
cd /d "%USERPROFILE%\Source"
)
)

看起来就很明显了,vs2017优先使用环境变量中的%VSCMD_START_DIR%,其次使用%USERPROFILE%\source,如果source目录不存在的话才会跑到当前目录。

解决方案有两种(随你喜好):
一、删除%USERPROFILE%\source目录
二、直接修改bat,注释或删除上面那段代码