如何以编程方式构建 Visual Studio 解决方案?
我想通过传递解决方案文件路径(.sln 文件)和构建模式(调试、发布)来编译解决方案。
我不想调用像 devenv.exe 或 msbuild.exe 这样的命令行进程。相反,我想使用 API 并了解是否存在编译错误。
是否可以?
如果您认为您知道如何做到这一点,请提供示例。
I would like to compile a solution by passing the solution file path (.sln file) and the build mode (debug, release).
I do not want to call a command line process like devenv.exe or msbuild.exe. Instead I would like to use an API and know if there were compile errors.
Is it possible?
Please provide samples if you think you know how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须在某个时刻启动一个进程来进行编译,因此您不妨生成一个进程来启动编译。这将比苦苦挣扎于 API 容易得多。
安德鲁就如何解决这个问题给出了一些很好的建议,但我仍然认为这是一个错误。
You're going to have to launch a process at some point to do the compilation, so you might as well spawn a process to kick off the compilation. It is going to be much easier than struggling with an API.
Andrew gives some good advice about how you might go about it, but I still think it is a mistake.
正如 itowlson 指出的那样,您还可以使用 构建引擎 API。
...来自 MSDN ...
您可以使用 MSBuild脚本,例如批处理文件。只需检查 ErrorLevel 是否为 0 以外的值...或者您可以享受 API 带来的乐趣。
C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
.. 来自 msbuild.exe /? ...
As itowlson pointed out, you could also use the Build Engine APIs.
... from MSDN ...
You can use MSBuild from a script such as a batch file. And just check the ErrorLevel for something other than 0... or you can have fun with the APIs.
C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
.. from msbuild.exe /? ...
这是一篇文章讨论以编程方式使用 C# 编译器。这是一篇讨论 SLN 文件的文章格式。 SLN 文件是一个 XML 文件,因此很容易阅读。
Here is an article discussing using the C# compiler programmatically. Here is an article discussing the SLN file format. The SLN file is an XML file, so it's pretty easy to read it.
我还建议使用 MSBuild API。看起来效果很好。然而,从 .NET 4 和 Visual Studio 2010 开始,它已经发生了显着变化。
I'd also suggest using the MSBuild API. It seems to work quite well. However, it has significantly changed as of .NET 4 and Visual Studio 2010.