Ziriax
Ziriax

Reputation: 1040

How to find the 'obj' directory for C# projects using Roslyn MSBuildWorkspace?

I'm using Roslyn to compile a C# solution with several projects in it, using the MsBuildWorkspace.

It's easy to find the output file of a Project, for that I can just use the OutputFilePath property.

But I can't find a way to figure out the 'intermediate' directory (typically this is the 'obj' directory, but this can be changed using the MSBuild properties BaseIntermediateOutputPath and IntermediateOutputPath in the csproj file).

Does anyone have an idea how to do this?

Upvotes: 1

Views: 557

Answers (1)

Sergey Vasiliev
Sergey Vasiliev

Reputation: 823

You can use Microsoft.Build.Evaluation.Project from Microsoft.Build.dll and got project properties:

void foo(String projectPath, IDictionary<String, String> globalProperties, String toolsVersion)
{
    Project project = new Project(projectPath, globalProperties, toolsVersion);
    String baseIntermediateOutputPath = GetProjectProperty(project, "BaseIntermediateOutputPath");
    String intermediateOutputPath = GetProjectProperty(project, "IntermediateOutputPath");
    // ....
}

static String GetProjectProperty(Microsoft.Build.Evaluation.Project project, String propertyName)
{
    return project.Properties
                  .FirstOrDefault(prop => String.Equals(prop.Name, propertyName, StringComparison.OrdinalIgnoreCase))
                 ?.EvaluatedValue;
}

Upvotes: 3

Related Questions