Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c# - Force BuildManager to use another version of MSBuild

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);

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 

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

Why is this happening in my code & how can I change it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...