3

Possible Duplicate:
How do i build a solution programatically in C#?

It's there a way to build a solution from a with c# code by an API or library??.

Because I only found how to do it by the command line in this link

Community
  • 1
  • 1
Jorge
  • 17,896
  • 19
  • 80
  • 126

3 Answers3

7

I don't know if this is what you're looking for, but here's an article that explains how to compile code programmatically:

http://support.microsoft.com/kb/304655

Here's some of the relevant code from that article:

using System.CodeDom.Compiler;
using System.Diagnostics;
using Microsoft.CSharp;

private void button1_Click(object sender, System.EventArgs e)
{
    CSharpCodeProvider codeProvider = new CSharpCodeProvider();
    ICodeCompiler icc = codeProvider.CreateCompiler();
    string Output = "Out.exe";
    Button ButtonObject = (Button)sender;

    textBox2.Text = "";
    System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
    //Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = true;
    parameters.OutputAssembly = Output;
    CompilerResults results = icc.CompileAssemblyFromSource(parameters, textBox1.Text);

    if (results.Errors.Count > 0)
    {
        textBox2.ForeColor = Color.Red;
        foreach (CompilerError CompErr in results.Errors)
        {
            textBox2.Text = textBox2.Text +
                        "Line number " + CompErr.Line +
                        ", Error Number: " + CompErr.ErrorNumber +
                        ", '" + CompErr.ErrorText + ";" +
                        Environment.NewLine + Environment.NewLine;
        }
    }
    else
    {
        //Successful Compile
        textBox2.ForeColor = Color.Blue;
        textBox2.Text = "Success!";
        //If we clicked run then launch our EXE
        if (ButtonObject.Text == "Run") Process.Start(Output);
    }
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
2

For quite a while now the way to build .NET solutions and projects is to use MSBuild.

Project and Solution files are MSBuild files.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

You can build your applications using MSBuild and passing the project as a parameter :

MSBuild.exe MyProj.proj /property:Configuration=Debug

You can start this process from C# using System.Diagnostics.Process

Nasreddine
  • 36,610
  • 17
  • 75
  • 94