Question

I am developing an application that would add/delete project properties and references from .csproj file as necessary.

project.AddItem("Reference", "System.IO", globalProperties);
project.AddItem("Reference", "System.Collections.Generic", globalProperties);

The above code adds the items. When I print out the ProjectItems in project.Items, I am able to see the added references. When I open the .csproj file that needs to be modified, the changes are not visible (since the copy of the file is being passed). However, I want to effect changes in the .csproj file that is being modified. I tried using the "ref" keyword, but doesn't work. Can someone tell me how to go about it?

Thanks.

Was it helpful?

Solution

you have o add first an item group before your reference like that

var slItemGroup = project.Xml.CreateItemGroupElement();
project.Xml.InsertAfterChild(slItemGroup, project.Xml.LastChild);
slItemGroup.AddItem("Reference", "System.IO");

and you have to call save method of your project:

project.Save(projectFileName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top