c# - 从代码构建 visual studio 解决方案

标签 c# visual-studio-2012 msbuild

我正在编写一个控制台应用程序以从 tfs 服务器获取解决方案,构建它并在 iis 上发布,但我一直停留在构建...

我找到了这段代码,它很有魅力

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)

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

最佳答案

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

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)
{              
    //...
}

关于c# - 从代码构建 visual studio 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22227804/

相关文章:

.net - 有没有办法使用新的 .NET SDK 项目系统禁用 globbing?

performance - 我们应该进行哪些硬件改进来加速我们的构建机器?

c# - 如何更改 StiWebViewer 的语言和文化?

vb6 - Visual Studio 2012 合并 VB6 代码会自动对代码进行错误更改

c# - 使用带有 ASP.NET 的 OpenXML SDK 在内存中流式传输 Word 文档会生成 "corrupt"文档

c++ - 在 Visual Studio 中添加对 C++ 项目的引用会产生什么后果?

c++ - 错误 C2065 : 'errno' : undeclared identifier in <string> in Visual Studio 2012

c# - 如何从 MSBuild 代码分析中获取 XML 报告

C# 打开新的 Excel 工作簿并添加数据

c# - DataGridView - 使用 DataPropertyName 显示子元素属性