我们正在修改一个在 Visual Studio 解决方案中重建所有内容的应用程序。我们使用的是 BuildEngine,但由于它已被弃用,我们将转向 Microsoft.Build。
代码是这样的:
var projectCollection = new Microsoft.Build.Evaluation.ProjectCollection();
var globalProperty = new Dictionary<String, String>();
var buildRequest = new Microsoft.Build.Execution.BuildRequestData(Directory.GetFiles(@"Build\", "*.sln").First(), globalProperty, null, new String[] { "Clean", "Build" }, null);
Microsoft.Build.Execution.BuildManager.DefaultBuildManager.Build(new Microsoft.Build.Execution.BuildParameters(projectCollection), buildRequest);
// 2nd time's a charm?
// Microsoft.Build.Execution.BuildManager.DefaultBuildManager.Build(new Microsoft.Build.Execution.BuildParameters(projectCollection), buildRequest);
但这些项目并没有被重建。但是,如果 Build 方法运行两次(如上面注释的代码),则项目将正确重建。