We recently merged two branches of code and ended up with duplicate entries for several of the files in the .csproj file. This seemed to happen to all the files which needed the
I used rodrigoff's .ps1 file (thanks!) but I had to modify it for my project to compile because it did not search for all entries in the csproj file, it was searching only for Compile and Content i changed this line of code:
$entries = $xml.Project.ItemGroup.Compile + $xml.Project.ItemGroup.Content | Group-Object Include
to:
$entries = $xml.Project.ItemGroup.Compile + $xml.Project.ItemGroup.Content +$xml.Project.ItemGroup.Reference + $xml.Project.ItemGroup.EmbeddedResource + $xml.Project.ItemGroup.None + $xml.Project.ItemGroup.Page + $xml.Project.ItemGroup.Resource + $xml.Project.ItemGroup.ProjectReference + $xml.Project.ItemGroup.Folder | Group-Object Include
What I ended up doing was to remove all the files from the project that were affected, refreshed the solution explorer and showed all files including ones no longer in the project, and then re-included the files to the project.
The first time I tried I didn't refresh the solution explorer and it didn't change anything, but after refreshing and toggling show/hide all files a few times after removing them from the project, including them back only added a reference to them once instead of the twice that I was seeing.
What really made this quick was that I could select all the files I wanted to work with by using shift-click and ctrl-click and then right clicking to perform whatever action on them as a group, such as excluding from the project, including in the project, or changing the copy to output directory setting.
In case someone else is looking for a way to do that automatically, I put together a script for this: RemoveCsProjDuplicates.ps1
The script will find all csproj files in the supplied folder, remove all duplicates and save the file.
To use just run
.\RemoveCsProjDuplicates.ps1 -filePath [Solution folder]
Feel free to use and adapt it to your needs