I am attempting to parse a solution and its projects using MSBuild SDK. The function below is able to successfully open the solution file and list containing projects.
I am using the latest version of VS 2022 targeting .NET 8 in a Console app. MSBuild NuGet package reference is: Microsoft.Build 17.10.4
.
public static void GenerateCodeMsBuild ()
{
var pathSolution = $@"D:...DomainBuilder.sln";
var solution = Microsoft.Build.Construction.SolutionFile.Parse(pathSolution);
Console.WriteLine($@"Solution: {solution}.");
foreach (var project in solution.ProjectsInOrder)
Console.WriteLine($@" - {project.ProjectName}.");
}
In order to gain access to further project elements such as references, folders, files, etc., I tried the Evaluation
namespace as follows:
public static void GenerateCodeMsBuild ()
{
var pathProject = $@"D:...DomainBuilder.ConsoleApp.csproj";
var projectOptions = new Microsoft.Build.Definition.ProjectOptions();
var project = Microsoft.Build.Evaluation.Project.FromFile(pathProject, projectOptions);
var projectInstance = Microsoft.Build.Execution.ProjectInstance.FromFile(pathProject, projectOptions);
}
Both above called to FromFile
result in the following exception:
InvalidProjectFileException (-2146233088): [The imported project "D:...DomainBuilder.ConsoleAppbinDebugnet8.0-windows10.0.22621.0CurrentMicrosoft.Common.props" was not found. Confirm that the expression in the Import declaration "D:...DomainBuilder.ConsoleAppbinDebugnet8.0-windows10.0.22621.0CurrentMicrosoft.Common.props" is correct, and that the file exists on disk. C:Program Filesdotnetsdk8.0.303SdksMicrosoft.NET.SdkSdkSdk.props].
I can verify that the project file is valid and is a newly created project from within VS.
I am guessing this has something to do the empty instance of ProjectOptions
and am not sure how to generate, obtain, or reference the Microsoft.Common.props
file. This file is not created when I create and build a new .NET 8 project from scratch.
Any advise would be appreciated.