通过代码构建Visual Studio解决方案 [英] Build visual studio solution from code

查看:108
本文介绍了通过代码构建Visual Studio解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个控制台应用程序,以从tfs服务器获取解决方案,进行构建并在iis上发布,但是我仍然坚持构建...

I'm writing a console application to get a solution from a tfs server, build it and publish on iis, but I'm stuck at building...

我找到了这段代码,就像一个吊饰一样

I found this code, which works like a charm

public static void BuildProject()
{
    string solutionPath = Path.Combine(@"C:\MySolution\Common\Common.csproj");

    List<ILogger> loggers = new List<ILogger>();
    loggers.Add(new ConsoleLogger());
    var projectCollection = new ProjectCollection();
    projectCollection.RegisterLoggers(loggers);
    var project = projectCollection.LoadProject(solutionPath);
    try
    {
        project.Build();
    }
    finally
    {
        projectCollection.UnregisterAllLoggers();
    }
}

但是我的解决方案相当大,包含多个相互依赖的项目(例如,项目A对项目B的引用)

but my solution it's pretty big and contains multiple projects which depends from each other (e.g. project A has a reference to project B)

如何获得正确的订单来构建每个项目? 有没有办法从.sln文件构建整个解决方案?

how to get the correct order to build each project? is there a way to build the entire solution from the .sln file?

推荐答案

尝试使用以下代码加载解决方案并进行编译:

Try using the following code to load a solution and compile it:

string projectFilePath = Path.Combine(@"c:\solutions\App\app.sln");

ProjectCollection pc = new ProjectCollection();

// THERE ARE A LOT OF PROPERTIES HERE, THESE MAP TO THE MSBUILD CLI PROPERTIES
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
globalProperty.Add("OutputPath", @"c:\temp");

BuildParameters bp = new BuildParameters(pc);
BuildRequestData buildRequest = new BuildRequestData(projectFilePath, globalProperty, "4.0", new string[] { "Build" }, null);
// THIS IS WHERE THE MAGIC HAPPENS - IN PROCESS MSBUILD
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
// A SIMPLE WAY TO CHECK THE RESULT
if (buildResult.OverallResult == BuildResultCode.Success)
{              
    //...
}

这篇关于通过代码构建Visual Studio解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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