从代码构建 Visual Studio 解决方案

2024-01-29

我正在编写一个控制台应用程序,以从 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)
{              
    //...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从代码构建 Visual Studio 解决方案 的相关文章

随机推荐