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

时间:2011-11-15 21:47:16

标签: c# asp.net

  

可能重复:
  How do i build a solution programatically in C#?

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

因为我只是通过命令行in this link

找到了解决方法

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:2)

现在很长一段时间构建.NET解决方案和项目的方法是使用MSBuild

项目和解决方案文件是MSBuild文件。

答案 2 :(得分:2)

您可以使用MSBuild构建应用程序并将项目作为参数传递:

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

您可以使用System.Diagnostics.Process

从C#开始此过程