MSBuild Programatically get property values from a project in a .sln file.

Steve Doiel 1 Reputation point
2022-02-16T19:22:19.863+00:00

I can open a solution file and iterate through all of the projects in a solution:

                    SolutionFile sf = SolutionFile.Parse(fileName);
                    LogText($"Solution {fileName} file loaded");
                    var projects = sf.ProjectsInOrder;
                    foreach (var project in projects)
                    {
                        LogText($"Project: \"{project.ProjectName}\" Type: {project.ProjectType}");
                    }

I would like to be able to get all of the properties for one projects in the solution.

The following fails:
ProjectCollection pc = new ProjectCollection();
Project project = pc.LoadProject(fileName,null);
var properties = project.Properties;
foreach (var property in properties)
{
LogText($"Property: \"{property.Name}\" Value: {property.UnevaluatedValue}");
}

Clearly something within MSBuild is able to get this information to build the solution, how can I get this information programatically?

Side note: the property manager in Visual Studio can show these values for C++ source files, but there appears to be nothing similar for C#.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,272 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.