BENBUN Coder
BENBUN Coder

Reputation: 4881

Updating a *.CSPROJ using MSBUILD API

Based on question : Reading a *.CSPROJ file in C#

I have code to extract some properties out of a *.csproj file, along the lines of :

Project project = new Project();

var Property001=
            from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
            from item in pg.Cast<BuildProperty>()
            where item.Name == "Property001"
            select item.Value.ToString();

This works fine, but the next question is how do I update the property using LINQ as well?

Upvotes: 2

Views: 1364

Answers (1)

Rup
Rup

Reputation: 34418

You could use LINQ to fetch the property item - rather than just the value - to update:

var Property001item =
        (from pg in project.PropertyGroups.Cast<BuildPropertyGroup>()
        from item in pg.Cast<BuildProperty>()
        where item.Name == "Property001"
        select item).FirstOrDefault();
if (Property001item != null)
{
    Property001item.Value = "MyNewValue";
}

Upvotes: 1

Related Questions