问题描述
我正在研究一些自动化.我有一个可以编译 .net 解决方案的批处理文件.我希望自动执行此操作,但我被卡住了.
I am working on some automation. I have a batch file that will compile .net solutions. I wish to automate this but I am stuck.
原始文件:
"B:\Microsoft Visual Studio 9.0\Common7\IDE\devenv" "My_Types\My_Types.sln" /build >> ..\Output\Build.txt
我可以让它与修改后的文件一起工作:
I can get this to work with a modified file:
Compile = New Process()
With Compile.StartInfo
.UseShellExecute = False
.RedirectStandardOutput = True
.FileName = """C:\Code\Intuitive Projects\Build Test.bat"""
End With
bSuccess = Compile.Start()
strOutput = Compile.StandardOutput.ReadToEnd()
Compile.WaitForExit()
MsgBox(strOutput)
修改后的文件
"B:\Microsoft Visual Studio 9.0\Common7\IDE\devenv" "C:\Code\Intuitive Projects\My_Types\My_Types.sln" /build
但我无法进行下一步工作.它与参数有关.
But I can't get the next step to work. It has something to do with the arguments.
Compile = New Process()
With Compile.StartInfo
.UseShellExecute = False
.RedirectStandardOutput = True
.FileName = "b:\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"
'.Arguments = "C:\Code\Intuitive Projects\My_Types\My_Types.sln /build" 'Does nothing
'.Arguments = """C:\Code\Intuitive Projects\My_Types\My_Types.sln"" /build" 'Does nothing
'.Arguments = """""C:\Code\Intuitive Projects\My_Types\My_Types.sln /build""""" 'Opens visual studio and parses the path as two files.
'.Arguments = """""""C:\Code\Intuitive Projects\Projects\My_Types\My_Types.sln"" /build""""" 'Opend the file but I get a message saying files can not be found but there are no files in the list.
'.Arguments = """""""C:\Code\Intuitive Projects\Projects\My_Types\My_Types.sln"" ""/build""""""" 'Tried this because I couldnt think of anything else, fails to find the file "/build"
End With
bSuccess = Compile.Start()
strOutput = Compile.StandardOutput.ReadToEnd()
Compile.WaitForExit()
MsgBox(strOutput)
推荐答案
Visual Studio 不是命令行程序,因此它无法输出到标准输出.但是,使用/Out "LogFilename.txt" 您可以将其输出到日志文件.例如:
Visual Studio is not a command line program, and as such it cannot output to the standard output. However with /Out "LogFilename.txt" you can make it output to a log file instead. For example:
"C:\Code\Intuitive Projects\My_Types\My_Types.sln" /build "Release|Any CPU" /Out C:\Temp\TempLog.txt
您可以打开并解析日志文件.
You can that open and parse the log file.
也就是说,如果可以,请使用 MSBuild.exe 而不是 DevEnv.exe.MSBuild 位于 C:\Windows\Microsoft.NET\Framework\v 或 C:\Windows\Microsoft.NET\Framework64\v 中的 64 位版本.它输出的例子告诉你所有你需要知道的:
That said, if you can, use MSBuild.exe instead of DevEnv.exe. MSBuild is located in C:\Windows\Microsoft.NET\Framework\v or the 64-bit version in C:\Windows\Microsoft.NET\Framework64\v. The examples that it outputs tell you all you need to know:
MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
MSBuild MyApp.csproj /t:Clean
/p:Configuration=Debug;TargetFrameworkVersion=v3.5
它像您期望的那样输出到标准输出/标准错误.唯一奇怪的是,如果在使用/p 参数发送的定义之一中包含括号或美元符号,MSBuild 会以非常奇怪的方式崩溃.在这些情况下,您应该使用 DevEnv.exe.
It outputs to the standard output / standard error like you expect. The only weird thing is that if you have parentheses or dollar signs in one of the definitions you send with the /p argument, MSBuild will freak out in very weird ways. In those cases you should use DevEnv.exe.
或者,您可以以编程方式调用 MSBuild,但这会涉及更多内容.请参阅有关 Microsoft.Build.Execution.BuildManager 的信息来执行此操作.如果需要,或者查看 CodeDOM达到非常低的水平.
Alternatively you can call MSBuild programmaticly this is a bit more involved however. See information on Microsoft.Build.Execution.BuildManager to do that. Or look at the CodeDOM if you want to get really low level.
这篇关于如何编译 .net 解决方案并获得输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!