How can I use an SLN file with MSBuild programmatically from C#?

969 Views Asked by At

I am trying to create a custom MSBuild script in C#, using the newer Microsoft.Build.Evaluation API. The problem I have is that this newer API does not support .sln files. The older deprecated Microsoft.Build.Engine API does support .sln files, but I'd like to use the newer one because 1) it's not deprecated and 2) there seems to be more online documentation and usage to reference. I've seen that MSBuild can create a .metaproj file when is successfully compiles a solution, when this assignment is made in CMD: set MSBuildEmitSolution=1. I need the .metaproj file to be able to compile the solution in the first place. Is there anything in the API for converting .sln to .metaproj? Is there any library out there for parsing .sln files?

1

There are 1 best solutions below

3
JamesFaix On BEST ANSWER

I figured it out after more searching. Finding good examples online is a little difficult because of the two different versions of the MSBuild API, and the popularity of just running MSBuild from the command line.

Here is the code that is now working for me, using the newer MSBuild API:

var pc = new ProjectCollection();

var parameters = new BuildParameters(pc)
{
    Loggers = new[] { _logger } //Instance of ILogger instantiated earlier
};

var request = new BuildRequestData(
    projectFullPath: pathToMySlnFile, //Solution file path
    globalProperties: myPropertyDictionary,
    toolsVersion: null,
    targetsToBuild: myTargetsArray,
    hostServices: null,
    flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);

var buildResult = BuildManager.DefaultBuildManager.Build(parameters, request);