I am currently working on an application that generates C# code files and adds them to an existing project. To edit the project I’m using Microsoft.Build.BuildEngine, and loading the exisiting .csproj file into the Project class.
csproj = new Project(new Engine(),"3.5");
csproj.Load("/myproject.csproj");
After wich I am able to add refeneces and compile items as I need. However I do not know if the files or references are already in the .csproj file, so currently they get added multiple times, which I do not want.
for example:
<Reference Include="System" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Core" />
Does anyone know of a way I can check if a builditem exists before I add it to the project? Or of a way to remove duplicated builditems after I’ve added them?
You could iterate through the EvaluatedItems of your Project Object
This sample code demonstrates how to check builditem existence before adding it :
And this one how to remove duplicates item