如何使用 C# 以编程方式构建解决方案?
如何以编程方式构建 C# 解决方案?
我应该能够传递解决方案的路径并获取输出消息(或者只是构建解决方案)。我如何在 C# 中实现这一目标?
我需要这个,因为我们正在为我们的项目构建一个单一的解决方案,而它现在从 SVN 获取所有内容并构建它。接下来将是一键部署所有内容。
How do I build a C# solution programmatically?
I should be able to pass the path of a solution and get the output messages (or just build the solution). How do I achieve this in C#?
I need this because we are building a single solution for our projects when it now gets everything from SVN and also builds it. Next would be deploying it all in one click.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请参阅.NET 4.0 MSBuild API 简介 使用 .NET 4.0 MSBuild API 的示例:
一个更简单的示例:
记住使用.NET 4 配置文件(而不是客户端配置文件)。
添加以下引用:System.XML、Microsoft.Build、Microsoft.Build.Framework 以及可选的 Microsoft.Build.Utilities.v4.0。
另请参阅 Stack Overflow 问题以编程方式运行 MSBuild。
要构建解决方案,请执行以下操作:
See .NET 4.0 MSBuild API introduction for an example using the .NET 4.0 MSBuild API:
A simpler example:
Remember to use the .NET 4 Profile (not the Client profile).
Add the following references: System.XML, Microsoft.Build, Microsoft.Build.Framework, and optionally Microsoft.Build.Utilities.v4.0.
Also look at Stack Overflow question Running MSBuild programmatically.
To build a solution, do the following:
大多数答案都提供了通过调用外部命令来完成此操作的方法,但是有一个 API,Microsoft.Build.Framework,通过 C# 构建。
博客文章中的代码:
请注意该博客文章中的代码可以工作,但有点过时。
Microsoft.Build.BuildEngine
已分解为几个部分。
Microsoft.Build.Construction
Microsoft.Build.Evaluation
Microsoft.Build.Execution
Most of the answers are providing ways to do it by calling external commands, but there is an API, Microsoft.Build.Framework, to build via C#.
Code from blog post:
Note the code in that blog post works, but it is a little dated. The
Microsoft.Build.BuildEngine
has been broken up into some pieces.
Microsoft.Build.Construction
Microsoft.Build.Evaluation
Microsoft.Build.Execution
您可以创建一个 .proj 文件:
然后使用项目文件作为参数调用 msbuild.exe。下面是一个批处理文件示例。在 C# 中,您可以调用 Process.Start,如其他海报所示。
You can create a .proj file:
And then call msbuild.exe using the project file as an argument. Below is a batch file example. From C#, you could call Process.Start as indicated by other posters.
您可以尝试博客文章使用 MSBuild API 控制 MSBuild。
You can try blog post Take control of MSBuild using MSBuild API.
如果您需要从 Visual Studio 扩展代码触发构建,则必须考虑 IVsBuildManagerAccessor 接口施加的限制 - 请参阅
一般说明,包括新的 IVsBuildManagerAccessor.docx
来自项目的托管包框架。
它的分支也可在在 GitHub< /a>.
If you need to trigger the build from Visual Studio extension code, you must consider the limitations imposed by IVsBuildManagerAccessor interface - see the
General Notes, including new IVsBuildManagerAccessor.docx
from Managed Package Framework for Projects.
Its fork is also available at GitHub.
当然,您可以使用 MSBuild 构建任何 Visual Studio 解决方案文件。
我相信您可以使用 Process.Start 使用适当的参数调用 MSBuild。
Surely you can use MSBuild to build any Visual Studio solution file.
I believe you can use
Process.Start
to invoke MSBuild with appropriate parameters.