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

查看:156
本文介绍了如何在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,

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

已被分成几部分.

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

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

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