如何以编程方式构建 Visual Studio 解决方案?

发布于 2024-08-10 13:53:58 字数 169 浏览 3 评论 0原文

我想通过传递解决方案文件路径(.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰ゝ天使的微笑 2024-08-17 13:53:58

您必须在某个时刻启动一个进程来进行编译,因此您不妨生成一个进程来启动编译。这将比苦苦挣扎于 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.

灼痛 2024-08-17 13:53:58

正如 itowlson 指出的那样,您还可以使用 构建引擎 API

...来自 MSDN ...

// Build a project file
bool success = engine.BuildProjectFile(@"c:\temp\validate.proj");

您可以使用 MSBuild脚本,例如批处理文件。只需检查 ErrorLevel 是否为 0 以外的值...或者您可以享受 API 带来的乐趣。

C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe

.. 来自 msbuild.exe /? ...

Microsoft (R) 构建引擎版本
2.0.50727.4927 [Microsoft .NET Framework,版本 2.0.50727.4927]
版权所有 (C) 微软公司
2005。保留所有权利。

语法:MSBuild.exe
[选项] [项目文件]

...

示例:

 MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
    MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug

As itowlson pointed out, you could also use the Build Engine APIs.

... from MSDN ...

// Build a project file
bool success = engine.BuildProjectFile(@"c:\temp\validate.proj");

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 /? ...

Microsoft (R) Build Engine Version
2.0.50727.4927 [Microsoft .NET Framework, Version 2.0.50727.4927]
Copyright (C) Microsoft Corporation
2005. All rights reserved.

Syntax: MSBuild.exe
[options] [project file]

...

Examples:

    MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
    MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug
拧巴小姐 2024-08-17 13:53:58

这是一篇文章讨论以编程方式使用 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.

夏日浅笑〃 2024-08-17 13:53:58

我还建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文