简体   繁体   中英

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?

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM