如何将外部文件读入 post 的变量中 - Visual Studio 中的构建事件?

How to read external file into a variable for post-build events in Visual Studio?

有没有办法在 Visual Studio post-build 事件中将外部文件的内容读入变量?

我想要这样做的原因是我们想要一个 post-build 事件,每个开发人员都运行该事件以将构建的程序集复制到某个本地 UNC 路径,但每个开发人员可能具有不同的 UNC 路径。所以我们不能把路径硬编码到项目文件中。如果我们可以读取外部文件,那么我们就可以在源代码管理中忽略它,只依赖于在该文件中设置路径值。

如果无法读取文件,是否有任何其他方法可以实现此目的?

据我所知,您无法在 VS post-build 事件中将外部文件读入变量。但是,您可以 运行 外部批处理文件并为其提供参数。这允许我们通过从源代码控制中排除此类脚本来为每个开发人员 post-build 批处理脚本,然后在 post-build 事件中检查它们是否存在并在它们存在时执行它们,如下所示:

CD "$(ProjectDir)"
IF EXIST postBuild.bat (
@ECHO Post-build script exists at: $(ProjectDir)postBuild.bat - executing...
CALL "$(ProjectDir)postBuild.bat" "$(TargetPath)" "$(TargetDir)"
)

显然批处理文件和 post-build 可以在以后扩展以在需要时传递更多参数。项目目录中的批处理文件 postBuild.bat 看起来像这样:

@REM **********
@REM Post-build script; assumes params:
@REM postBuild.bat "$(TargetPath)" "$(TargetDir)"
@REM **********

@ECHO TargetPath: %1
@ECHO TargetDir: %2

[...]