如何在 C# 中以编程方式构建解决方案? [英] How do I build a solution programmatically in C#?

查看:22
本文介绍了如何在 C# 中以编程方式构建解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I build a C# solution programmatically?

我应该能够传递解决方案的路径并获取输出消息(或者只是构建解决方案).我如何在 C# 中实现这一点?

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

我需要这个,因为我们正在为我们的项目构建一个单一的解决方案,而它现在从 SVN 获取所有内容并构建它.接下来是一键部署.

I need this because we are building a single solution for our projects when it now gets everything from SVN and also builds it. Next would be deploying it all in one click.

推荐答案

大多数答案都提供了通过调用外部命令来实现的方法,但是一个 API,Microsoft.Build.Framework,通过 C# 构建.

Most of the answers are providing ways to do it by calling external commands, but there is an API, Microsoft.Build.Framework, to build via C#.

来自博文的代码:

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 post works, but it is a little dated. The

Microsoft.Build.BuildEngine

已经被分解成一些碎片.

has been broken up into some pieces.

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

这篇关于如何在 C# 中以编程方式构建解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆