我如何在C#中以编程方式构建解决方案?

问题描述 投票:31回答:7

我如何以编程方式构建C#解决方案?

我应该能够传递解决方案的路径并获取输出消息(或只是构建解决方案)。如何在C#中实现这一目标?

c# msbuild build project solution
7个回答
22
投票

你们中的大多数人都提供了通过调用外部命令来做到这一点的方法,但是有[[is一个通过C#构建的api Microsoft.Build.Framework


来自博客的代码:

using Microsoft.Build.BuildEngine; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; public class SolutionBuilder { BasicFileLogger b; public SolutionBuilder() { } [STAThread] public string Compile(string solution_name,string logfile) { b = new BasicFileLogger(); b.Parameters = logfile; b.register(); Microsoft.Build.BuildEngine.Engine.GlobalEngine.BuildEnabled = true; Project p = new Project (Microsoft.Build.BuildEngine.Engine.GlobalEngine); p.BuildEnabled = true; p.Load(solution_name); p.Build(); string output = b.getLogoutput(); output += “nt” + b.Warningcount + ” Warnings. “; output += “nt” + b.Errorcount + ” Errors. “; b.Shutdown(); return output; } } //The above class is used and compilation is initiated by the following code, static void Main(string[] args) { SolutionBuilder builder = new SolutionBuilder(); string output = builder.Compile(@”G:CodesTestingTesting2web1.sln”, @”G:CodesTestingTesting2build_log.txt”); Console.WriteLine(output); Console.ReadKey(); }

请注意该博客中的代码有效,但过时了

Microsoft.Build.BuildEngine

已分解成几块

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution


30
投票
请参阅此链接,以获取使用.NET 4.0 MSBuild API的示例:

http://www.odewit.net/ArticleContent.aspx?id=MsBuildApi4&format=html

List<ILogger> loggers = new List<ILogger>(); loggers.Add(new ConsoleLogger()); var projectCollection = new ProjectCollection(); projectCollection.RegisterLoggers(loggers); var project = projectCollection.LoadProject(buildFileUri); // Needs a reference to System.Xml try { project.Build(); } finally { projectCollection.UnregisterAllLoggers(); }

一个简单的例子:

var project = new Project(buildFileUri, null, "4.0"); var ok = project.Build(); // or project.Build(targets, loggers) return ok;

记住要使用.NET 4配置文件(而不是客户端配置文件)。

添加以下参考:System.XML,Microsoft.Build,Microsoft.Build.Framework和可选的Microsoft.Build.Utilities.v4.0。

也请看这里:

running msbuild programmatically

要构建解决方案,请执行以下操作:

var props = new Dictionary<string, string>(); props["Configuration"] = "Release"; var request = new BuildRequestData(buildFileUri, props, null, new string[] { "Build" }, null); var parms = new BuildParameters(); // parms.Loggers = ...; var result = BuildManager.DefaultBuildManager.Build(parms, request); return result.OverallResult == BuildResultCode.Success;


5
投票
// Fix to the path of your msbuild var pathToMsBuild = "C:\\Windows\\DotNet\\Framework\\msbuild.exe"; Process.Start(pathToMsBuild + " " + pathToSolution);

2
投票
您可以创建一个.proj文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <!-- Common --> <Solution Include="Common\Util\Util.sln"/> <Solution Include="Common\EventScheduler\EventSchedulerSolution\EventSchedulerSolution.sln"/> <!-- Server --> <Solution Include="Server\DataLayer\DataTransferObjects\SharedModel\SharedModel.sln"/> <Solution Include="Server\DataLayer\DataTier\ESPDAL.sln"/> <!-- Internal Tools --> <Solution Include="InternalTools\ServerSchemaUtility\ServerSchemaUtility.sln"/> </ItemGroup> <Target Name="Rebuild"> <MSBuild Projects="@(Solution)" Targets="Rebuild" Properties="Configuration=Release"/> </Target> </Project>

然后使用proj文件作为参数调用msbuild.exe,下面是一个批处理文件示例。在C#中,您可以按照其他发布者的指示调用Process.Start。

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" BuildSolutions.proj pause



1
投票
如果需要从Visual Studio扩展代码中了解该版本,则必须考虑IVsBuildManagerAccessor接口所施加的限制-请参阅一般说明,包括新的IVsBuildManagerAccessor.docx从Managed Package Framework for Projects开始。也可以在github上使用它的前叉。

在带有MSBuild 4.0的Visual Studio 2010中,有新的交互在解决方案构建管理器和MSBuild之间影响项目使用这些服务的系统。 MSBuild 4.0包含一个新的称为Build Manager的组件(请勿与解决方案构建管理器(是VS组件),用于控制提交构建请求。由于Visual Studio,这变得必要现在,2010年允许进行并行构建(特别是本机项目),并且需要调解对共享资源(例如CPU)的访问。对于以前简称为Project.Build()的项目系统调用构建必须进行一些更改。现在的项目系统必须:

    使用IServiceProvider接口询问SVsBuildManagerAccessor服务。该操作应在项目系统已加载,很早就可以进行任何构建。
  1. 如果需要UI线程,请通知系统
  2. 如果要进行设计时构建,请通知系统。
  3. 使用构建管理器访问器注册其记录器。
  4. 直接将构建请求提交给MSBuild Build Manager,而不是在Project上调用方法。

0
投票
您当然可以使用msbuild来构建任何Visual Studio解决方案文件。

我相信您可以使用Process.Start来调用具有适当参数的msbuild。

© www.soinside.com 2019 - 2024. All rights reserved.