問題描述
以下代碼嘗試使用 BuildManager
以編程方式構(gòu)建解決方案:
The following code tries to build a Solution programmatically, using BuildManager
:
ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "12.0";
pc.Loggers.Add(fileLogger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(solutionName, globalProperty, null, new[] { "Build" }, null);
BuildParameters buildParameters = new BuildParameters(pc)
{
DefaultToolsVersion = "12.0",
OnlyLogCriticalEvents = false,
DetailedSummary = true,
Loggers = new List<Microsoft.Build.Framework.ILogger> { fileLogger }.AsEnumerable()
};
var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);
當(dāng)我運(yùn)行這段代碼時(shí),它不會構(gòu)建任何東西.我可以看到除了 winmdexp.exe
的一個(gè)特定版本之外,還使用了以下編譯器 csc.exe
:
When I run this code, it doesn't build anything. I can see that the following compiler csc.exe
is being used, in addition to one particular version of winmdexp.exe
:
C:WINDOWSMicrosoft.NETFrameworkv4.0.30319Csc.exe
ExportWindowsMDFile:
C:Program Files (x86)Microsoft SDKsWindowsv8.0AinNETFX 4.0 Toolswinmdexp.exe
但是當(dāng)我使用 VS IDE 成功構(gòu)建解決方案時(shí),出現(xiàn)以下信息:
But when I successfully build a solution using VS IDE, the following information comes up:
C:Program Files (x86)MSBuild12.0inCsc.exe
ExportWindowsMDFile:
C:Program Files (x86)Microsoft SDKsWindowsv8.1AinNETFX 4.5.1 Toolswinmdexp.exe
為什么在我的代碼中會發(fā)生這種情況?怎么改?
Why is this happening in my code & how can I change it?
推薦答案
截至 Visual Studio 2013,MSBuild 不再是 .NET Framework 組件.這意味著對打包和部署進(jìn)行了一些重組.舊的 MSBuild 框架仍然存在于 .NET Framework 文件夾中.但是,當(dāng)您安裝 Visual Studio 2013 時(shí),Microsoft Build Tools 12.0 也會安裝到 C:Program Files (x86)MSBuild12.0.請注意,構(gòu)建工具與 Visual Studio 分開提供此處.
As of Visual Studio 2013, MSBuild is no longer a .NET Framework component. This meant some restructuring of packaging and deployment. The old MSBuild framework still lives in the .NET Framework folders. However when you install Visual Studio 2013, the Microsoft Build Tools 12.0 are also installed to C:Program Files (x86)MSBuild12.0. Note that the Build Tools are available separately from Visual Studio here.
當(dāng)我第一次嘗試這個(gè)時(shí),我也遇到了和你一樣的情況.問題是您可能已經(jīng)引用了舊的4.0"MSBuild Framework 程序集.您需要引用位于 C:Program Files (x86)MSBuild12.0in 中的新 12.0 程序集(您必須在 VS 中瀏覽以添加引用).您可能需要 Microsoft.Build、M??icrosoft.Build.Engine 和 Microsoft.Build.Framework.更新這些參考資料后,我發(fā)現(xiàn)它在構(gòu)建時(shí)使用了與 VS 2013 相同的工具.
I encountered the same situation as you as well when I first tried this. The issue is that you probably have referenced the old "4.0" MSBuild Framework assemblies. You need to reference the new 12.0 assemblies located in C:Program Files (x86)MSBuild12.0in (you'll have to browse there in VS to add the references). You probably need Microsoft.Build, Microsoft.Build.Engine, and Microsoft.Build.Framework. Once I updated those references I saw it was using the same tools as VS 2013 when building.
這篇關(guān)于強(qiáng)制 BuildManager 使用另一個(gè)版本的 MSBuild的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!