Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and remove duplicate entries in csproj file

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 Copy to Output Directory to be changed to Copy always after that change was made and merged with the other branch.

I was wondering if this is going to cause any unexpected problems if it is left like this and what I should do to fix it. It's a lot of files so I don't want to go through and do it for each file manually. It is also not all of the files, but it is a significant number of files. What should I do?

like image 706
Patrick Graham Avatar asked Jun 24 '16 19:06

Patrick Graham


2 Answers

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

like image 127
rodrigoff Avatar answered Nov 05 '22 19:11

rodrigoff


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.

like image 21
Patrick Graham Avatar answered Nov 05 '22 19:11

Patrick Graham