使用Microsoft.Build构建.sln文件

时间:2014-12-14 17:56:57

标签: c# build msbuild microsoft.build

我正在使用VS2012,我想使用c#以编程方式构建.sln文件。我知道我需要使用Microsoft.Build api这样做。我已经建立了解决方案

string slnPath= @"C:\Path\To\solution.sln";

ProjectCollection pc = new ProjectCollection();
List<ILogger> loggers = new List<ILogger>();
loggers.Add(new ConsoleLogger());

Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();

GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "Win32");
BuildRequestData BuildRequest = new BuildRequestData(projectFileName, GlobalProperty, "4.0", new string[] { "Build" }, null);
BuildParameters bp = new BuildParameters(pc);
bp.Loggers = loggers;
BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, BuildRequest);

这会执行build命令,但是我收到错误消息:

error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

在.vcxproj中,产生错误的行是:

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

我已尝试将环境变量VCTargetsPath设置为

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\

但是我得到一个错误:

error MSB4127: The "SetEnv" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Build.CppTasks.Common.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Micros

oft.Build.Framework。

有没有人对我如何解决此问题有任何建议? 编辑:我看到这似乎是Visual Studio中的一个错误,但我无法重现评论中提到的问题。如果运行Developer命令提示符,我可以使用MSBuild构建解决方案。

0 个答案:

没有答案