Is it possible to extract the value of element in csproj file with dotnet CLI?

Viewed 165

csproj file contains project properties which I'd like to use in CI/CD pipeline.

Is it possible to extract those values with dotnet cli (or some other standard tool) without parsing xml via some standalone script?

For example having the project:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <Version>1.0.0</Version>
        <GenerateDocumentationFile>true</GenerateDocumentationFile>
    </PropertyGroup>
</<Project>

I need to resolve the value of <Version>.

It's also wroth noting that project may contain conditional elements, and ideally it would be nice to resolve properties in the context of predefined variables.

1 Answers

MSBuild is very extensible. You could use a target to write the version number to a file:

  <Target Name="WriteVersion" AfterTargets="Build">
    <WriteLinesToFile Lines="$(Version)"
                      File="$(IntermediateOutputPath)version.txt" />
  </Target>

That would write a version.txt file to a folder such as obj/Debug/net5.0 (depending on the Configuration and TargetFramework).

Also be sure to check out https://msbuildlog.com/ for how to investigate / debug builds.

Related