peplamb peplamb - 1 year ago 131
C# Question

How do i build a solution programmatically in C#?

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

Nix Nix
Answer Source

Most of you all 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:

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 works, but is a little dated the

Microsoft.Build.BuildEngine

has been broken up into some pieces

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download