Building a .sln file with Microsoft.Build

1.3k views Asked by At

I'm using VS2012 and I want to build a .sln file programatically using c#. I understand I need to use the Microsoft.Build api to do so. I've gotten as far as building the solution

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

this executes the build command, but I get an error saying:

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.

Inside the .vcxproj the line that gives the error is:

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

I've tried setting the Environment Variable VCTargetsPath to be

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

But I then get an error:

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.

Does anyone have any suggestions as to how I can fix this? EDIT: I see this appears to be a bug in Visual studio, but I can't reproduce the issue mentioned in the comment. If run a Developer command prompt, I can build solutions using MSBuild.

0

There are 0 answers