[ACCEPTED]-How do I build a solution programmatically in C#?-solution

Accepted answer
Score: 30

See .NET 4.0 MSBuild API introduction for an example using the .NET 4.0 MSBuild 5 API:

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();
}

A simpler example:

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

Remember to use the 4 .NET 4 Profile (not the Client profile).

Add the following references: System.XML, Microsoft.Build, Microsoft.Build.Framework, and 3 optionally Microsoft.Build.Utilities.v4.0.

Also 2 look at Stack Overflow question Running MSBuild programmatically.

To build 1 a solution, do the following:

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;
Score: 23

Most of the answers are providing ways to 5 do it by calling external commands, but 4 there is an API, Microsoft.Build.Framework, to build via C#.


Code from 3 blog post:

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();
}

Note the code in that blog post 2 works, but it is a little dated. The

Microsoft.Build.BuildEngine

has 1 been broken up into some pieces.

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

Score: 5
// Fix to the path of your MSBuild executable
var pathToMsBuild = "C:\\Windows\\DotNet\\Framework\\msbuild.exe";

Process.Start(pathToMsBuild + " " + pathToSolution);

0

Score: 2

You can create a .proj file:

<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>

And then call 4 msbuild.exe using the project file as an 3 argument. Below is a batch file example. From 2 C#, you could call Process.Start as indicated 1 by other posters.

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

pause
Score: 2
Score: 1

If you need to trigger the build from Visual 19 Studio extension code, you must consider 18 the limitations imposed by IVsBuildManagerAccessor 17 interface - see the General Notes, including new IVsBuildManagerAccessor.docx from 16 Managed Package Framework for Projects. Its fork is also available at GitHub.

In Visual 15 Studio 2010 with MSBuild 4.0, there are 14 new interactions between the Solution Build 13 Manager and MSBuild which impact project systems 12 utilizing these services. MSBuild 4.0 contains 11 a new component called the Build Manager 10 (not to be confused with the Solution Build 9 Manager which is a VS component) which controls 8 the submission of build requests. This 7 became necessary as Visual Studio 2010 now 6 allows parallel builds to occur (notably 5 native projects) and access to shared resources 4 such as the CPU needed to be mediated. For project 3 systems which previously simply called Project.Build() to invoke 2 a build several changes must be made. The 1 project system now must:

  1. Ask for the SVsBuildManagerAccessor service using the IServiceProvider interface. This should be done soon after the project system is loaded, well before any builds might occur.
  2. Notify the system if you need the UI thread
  3. Notify the system if you are doing a design-time build.
  4. Register its loggers using the Build Manager Accessor.
  5. Submit build requests directly to the MSBuild Build Manager, rather than invoking a method on the Project.
Score: 0

Surely you can use MSBuild to build any 3 Visual Studio solution file.

I believe you 2 can use Process.Start to invoke MSBuild with appropriate 1 parameters.

More Related questions