visual-studio 如何使用批处理文件构建解决方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2729535/
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
How to build solution using batchfile
提问by Ram
I want to build .NET solution using batch file.
我想使用批处理文件构建 .NET 解决方案。
I am aware that I need to use following statement
我知道我需要使用以下语句
devenv /build release "D:\Source Code\Source\test.sln"
But I do not know how to create batch file which will execute on VS command prompt.
但我不知道如何创建将在 VS 命令提示符下执行的批处理文件。
回答by Simon P Stevens
The visual studio command prompt just loads some variables and path settings. That's all it is, it's nothing specially, it's not a different command prompt, it's the same command prompt with some settings configured. You can load the same settings in your own batch file by including the following line at the top:
Visual Studio 命令提示符只加载一些变量和路径设置。就是这样,没什么特别的,它不是一个不同的命令提示符,它是配置了一些设置的相同命令提示符。通过在顶部包含以下行,您可以在自己的批处理文件中加载相同的设置:
call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86
(Obviously, for different versions of VS, the path might change slightly)
(显然,对于不同版本的VS,路径可能略有变化)
You can replace "x86" with the appropriate architecture for your machine. The permitted values are:
您可以使用适合您机器的架构替换“x86”。允许的值为:
- x86
- amd64
- x64
- ia64
- x86_amd64
- x86_ia64
- x86
- amd64
- x64
- ia64
- x86_amd64
- x86_ia64
That said, I don't think you actually need to load all the vars/paths all you really need to do is provide the full path to the devenv.exefile. You could try this instead:
也就是说,我认为您实际上不需要加载所有变量/路径,您真正需要做的就是提供devenv.exe文件的完整路径。你可以试试这个:
"c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe" /build release "D:\Source Code\Source\test.sln"
(Again, the path will change for different versions of visual studio)
(同样,不同版本的visual studio 路径会有所不同)
回答by Joe
The sample batch file below will detect the install directory that contains devenv.exe by looking it up in the registry (for VS2005, can easily be adapted for other versions) and executes devenv.exe. Is this what you're looking for?
下面的示例批处理文件将通过在注册表中查找包含 devenv.exe 的安装目录(对于 VS2005,可以轻松适应其他版本)并执行 devenv.exe。这是你要找的吗?
@echo off
CALL :GETVS2005DIR
IF "%VS2005DIR%" == "" GOTO NOVS2005
IF NOT EXIST "%VS2005DIR%" GOTO NOVS2005
%VS2005DIR%devenv.exe ...
GOTO :EOF
:GETVS2005DIR
for /f "tokens=1,2* delims= " %%i in ('reg query HKLM\Software\Microsoft\VisualStudio.0 /v InstallDir') do set VS2005DIR=%%k
GOTO :EOF
:NOVS2005
echo.
echo Visual Studio 2005 installation directory not found
echo.
GOTO :EOF
Note also that as long as your solution does not contain a Setup project, you will normally be able to build it using MSBUILD, which is simpler and works on a machine without Visual Studio installed:
另请注意,只要您的解决方案不包含安装项目,您通常就可以使用 MSBUILD 构建它,它更简单并且可以在没有安装 Visual Studio 的机器上运行:
REM Check MsBuild is available (this is for V2.0, use a different version if desired)
SET MSBUILD=%WINDIR%\Microsoft.NET\Framework\v2.0.50727\MsBuild.exe
IF NOT EXIST "%MSBUILD%" GOTO NOMSB
"%MSBUILD%" MySolution.sln /t:rebuild /p:configuration=Debug /verbosity:quiet
GOTO :EOF
:NOMSB
echo.
echo MSBUILD not found
echo.
GOTO :EOF
回答by Hans Olsson
Not sure if I understand the question.
不知道我是否理解这个问题。
Just create a file called test.bat, add the statement you've written above to that file and then just open a VS command prompt and type [pathtobatfile]\test.bat.
只需创建一个名为 test.bat 的文件,将您在上面编写的语句添加到该文件中,然后打开 VS 命令提示符并键入 [pathtobatfile]\test.bat。

