Creating a .sln File Using Microsoft.Build

I am using VS2012 and I want to programmatically create a .sln file using C #. I understand that for this you need to use the Microsoft.Build api. I got to building a 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:

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 line that gives the error:

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

I tried to set the VCTargetsPath environment variable

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

But I get an error message:

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 on how I can fix this? EDIT: I see that this seems like a bug in Visual Studio, but I cannot reproduce the issue mentioned in the comment. If I run the Developer command line, I can create solutions using MSBuild.

+4

All Articles