This post covers how to remove missing file references within a visual studio project file. Unfortunately, there isn’t anything available in visual studio, however, I was able to get things done with a little bit of PowerShell magic.
Cleaning up csproj files – Remove missing references
I recently worked with a couple of visual studio projects containing missing file references (old stylesheets images and script files). In general, this won’t break the build unless you have to publish your solution using MSBuild. This because publish requires that all the files included within the project exist on disk.
For old web related projects, this might be an issue because images/stylesheets /scripts etc might be missing for various reasons. People often clean up files within the source control repository and forgetting to update or checking in the updated csproj file. Building a project with missing references will not trigger any errors, allowing you to build up clutter over time.
A dirty solution is available on StackOverflow which can be found here:
Remove VS2010 references to deleted files in web project. The steps within this article simply regenerate the include information within your project file. However, the problem is that this will include all files in the project folder. Even files that are part of your software solution but not need to belong within Visual Studio.
To resolve all of this, I have created the script below.
The idea is simple; just go through all content includes elements and verify if the file still exists on disk.
what to look for ->
Calling the function is easy as shown below. This sample also generates a clean project file based on the output.
To avoid surprises, I have also included a report option. This option allows you to list all missing references.
Hopefully, this will save others some time.