Search code examples
c#visual-studio-2017

How to read csproj xml elements in runtime?


I am trying to read specific XML elements from a csproj file (my project file), specifically the <ApplicationVersion> element (I'm deploying under ClickOnce - (this is not the assembly version)

Is there a runtime option to read this field? or the only option is to simply parse the csproj file as an xml file?


Solution

  • I eventually did the following:

    I used Ian Kemp's advice (comment) to install Microsoft.Build, Microsoft.Build.Utilities.Core and Microsoft.Build.Framework nuget packages.

    And then used the following code to load the csproj file to memory and get a specific attribute value as string

    Project project = new Project(Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.FullName, "<my_project>.csproj"));
    Version application_version = Version.Parse(project.GetPropertyValue("ApplicationVersion"));