3

Somehow I am seeing duplicate files in my solution. I dig into it and found that somehow, there are two entries for some files in .csproj file. That's why two files are shown that is actually one file. I have to find for all the files in .csproj file. Is there any easy way to remove those entries from csproj? And also can anyone let me know the cause of this?

DhavalR
  • 1,409
  • 3
  • 29
  • 57

1 Answers1

1

As per this question and this other one on StackOverflow, this might happen when TFS (or other version control systems) edit a project file e.g. during a merge operation.

Anyway, you have probably opened a csproj file and seen that it's just an XML, with files being listed in elements like those below:

<ItemGroup>
   <Compile Include="Controllers\AccountController.cs" />
   <Compile Include="Controllers\ContactsController.cs" />
   <Compile Include="Controllers\HomeController.cs" />
   ...
</ItemGroup>

One answer to the question I linked above also provides a reference to a deduplication Powershell script on GitHub, which I haven't tried and cannot guarantee for, although I don't see what harm it could do.

If your files are not too many, you can just look for the following string

.cs" />

with a text editor like PsPad, have it list the results and remove double entries by hand. Of course, before doing anything, backup your file.

Another solution involves selecting all duplicated files in your project, clicking "Exclude from project" and then "Show all files" and include them by hand.

Francesco B.
  • 2,729
  • 4
  • 25
  • 37
  • How about copying file in a simple text file, read that file through code, find duplicate entries using code (simple linq or anything).? – DhavalR Apr 06 '18 at 12:22
  • Of course you can, check [this question](https://stackoverflow.com/questions/19218163/parse-csproj-via-c-sharp-cannot-parse-itemgroup?rq=1&utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) to begin with. Personally I would resort to writing code only if you have lots of duplicates (>30) or if this problem is recurring. – Francesco B. Apr 06 '18 at 13:12
  • Right now I just deleted those files from project and included them again. – DhavalR Apr 09 '18 at 09:28
  • My project is quite complex. If I have to manually go through the project and remove duplicates, so be it. But my concern is that I still don't know what triggered the duplication and it will just happen again. – Kevin Burton Nov 15 '19 at 15:23
  • Also with projects that are a combination of ASP.NET and Angular the project file doesn't contain references to the individual files that are apart of the client side code that are duplicated. I am not sure what is causing the duplication in this case. – Kevin Burton Nov 15 '19 at 15:31
  • It seems to have to do with lines in the .csproj like these: – Kevin Burton Nov 15 '19 at 15:57
  • Without restarting VS 2022, I excluded from project all duplicated files and clicked twice at **Show All Files**; they gone. – Marcelo Scofano Diniz Nov 27 '22 at 14:24