【问题标题】:How do I build a solution programmatically in C#?如何在 C# 中以编程方式构建解决方案?
【发布时间】:2011-09-24 13:54:06
【问题描述】:

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

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

我需要这个,因为我们正在为我们的项目构建一个单一的解决方案,它现在从 SVN 获取所有内容并构建它。接下来是一键部署。

【问题讨论】:

标签: c# msbuild build project solution


【解决方案1】:

您当然可以使用 MSBuild 构建任何 Visual Studio 解决方案文件。

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

【讨论】:

    【解决方案2】:
    // Fix to the path of your MSBuild executable
    var pathToMsBuild = "C:\\Windows\\DotNet\\Framework\\msbuild.exe";
    
    Process.Start(pathToMsBuild + " " + pathToSolution);
    

    【讨论】:

      【解决方案3】:

      大多数答案都是通过调用外部命令来提供方法,但一个 API,Microsoft.Build.Framework,可以通过 C# 构建。


      博文中的代码:

      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

      【讨论】:

      • 看起来像“使用 MSBuild 通过 C# 自动编译 Visual Studio 项目”链接已损坏
      • 此信息已过时 - 如果您使用 Engine 类,您将收到以下警告:“此类已被弃用。请改用 Microsoft.Build 程序集中的 Microsoft.Build.Evaluation.ProjectCollection ."。所以下面使用 Project 和 ProjectCollection 的答案是正确的答案。
      • 链接(实际上)已损坏。它重定向到一个通用页面,".NET API Browser".
      • 指的是什么博文?它的链接是什么? (回复editing your answer,不在 cmets 中(视情况而定)。)
      【解决方案4】:

      您可以创建一个 .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>
      

      然后使用项目文件作为参数调用 msbuild.exe。下面是一个批处理文件示例。从 C# 中,您可以调用 Process.Start,如其他海报所示。

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

      【讨论】:

        【解决方案5】:

        有关使用 .NET 4.0 MSBuild API 的示例,请参阅 .NET 4.0 MSBuild API introduction

        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、M​​icrosoft.Build.Framework 和可选的 Microsoft.Build.Utilities.v4.0。

        另请查看 Stack Overflow 问题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;
        

        【讨论】:

          【解决方案6】:

          【讨论】:

            【解决方案7】:

            如果您需要从 Visual Studio 扩展代码触发构建,则必须考虑 IVsBuildManagerAccessor 接口施加的限制 - 请参阅 一般说明,包括新的 IVsBuildManagerAccessor.docx 来自Managed Package Framework for Projects。 它的叉子也可用at GitHub

            在带有 MSBuild 4.0 的 Visual Studio 2010 中,有新的交互 解决方案构建管理器和影响项目的 MSBuild 之间的关系 使用这些服务的系统。 MSBuild 4.0 包含一个新的 称为构建管理器的组件(不要与 解决方案构建管理器,它是一个 VS 组件),它控制 提交构建请求。这成为了必要的 Visual Studio 2010 现在允许并行构建(尤其是本地项目)和 对 CPU 等共享资源的访问需要进行调解。为了 以前简单地调用 Project.Build() 的项目系统 调用构建必须进行一些更改。现在的项目系统 必须:

            1. 使用 IServiceProvider 接口请求 SVsBuildManagerAccessor 服务。这应该在后不久完成 项目系统已加载,远在任何构建发生之前。
            2. 如果需要 UI 线程,请通知系统
            3. 如果您正在进行设计时构建,请通知系统。
            4. 使用构建管理器访问器注册其记录器。
            5. 直接向 MSBuild 构建管理器提交构建请求,而不是调用项目上的方法。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-12-15
              • 1970-01-01
              • 2012-09-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-09-22
              相关资源
              最近更新 更多