【问题标题】:Build a C# solution programatally [duplicate]以编程方式构建 C# 解决方案 [重复]
【发布时间】:2011-11-15 21:47:16
【问题描述】:

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

有没有一种方法可以通过 API 或库使用 c# 代码构建解决方案??

因为我只是通过命令行in this link找到了怎么做

【问题讨论】:

  • 请问,你为什么需要这个?
  • 因为我需要在运行时创建一个.DLL并添加到项目模板中

标签: c# asp.net


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      您可以使用 System.Diagnostics.Process 从 C# 启动此过程

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-24
        • 2012-09-21
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 1970-01-01
        • 2014-12-15
        • 1970-01-01
        相关资源
        最近更新 更多