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

Jor*_*rge 3 c# asp.net

可能重复:
如何在C#中以编程方式构建解决方案?

有一种方法可以通过API或库从c#代码构建解决方案.

因为我只是通过此链接中的命令行找到了该怎么做

Jam*_*son 9

我不知道这是否是您正在寻找的,但这里有一篇文章解释了如何以编程方式编译代码:

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

以下是该文章中的一些相关代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)