我们正在修改在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方法运行两次(如上面的注释代码),则可以正确地重建项目。