Reputation: 5821
The problem- We can parse a csproj file in some ways, but most of the information is not in the file, but is either by default or affected by other properties.
I want to work against csproj files and solution- get project dependencies, get properties and items (things like 'TargetFramework', compiled files..)
The TargetFramework can be a tag with value, or 'TargetFrameworks' tag with multi-values that parsed to 'TargetFramework'.
The old solution- MSBuild provide a microsoft.build.evaluation library to work with csproj file, but this library compiled to net471, and cause errors when we use it in netcoreapp.
What will be the solution for the problem, in .net core projects?
Upvotes: 6
Views: 4887
Reputation: 433
For .Net Core Microsoft.Build
NuGet Package works as below:
var projectRootElement = ProjectRootElement.Open(csprojPath);
projectRootElement.AddProperty("Version", "3.4.5");
projectRootElement.Save();
If Version exists it overrides it. If not exist adds new property name is "Version" and value is "3.4.5".
Also you can get all properties which defined in .csproj
var projectRootElement = ProjectRootElement.Open(csprojPath);
foreach (var property in projectRootElement.Properties)
{
Console.WriteLine($"Name: {property.Name} - Value: {property.Value}");
}
Tested in .NetCore 5.0
Upvotes: 10