Remove VS2010 references to deleted files in web project

I just had this problem in VS 2015. Files were missing all over the web project, so didn't want to go looking for them all.

The quickest way home was to: exclude all files/folders then include them all again.

That is:

  1. solution explorer -> select all files and folders -> right-click -> "Exclude from Project"
  2. solution explorer -> click "Show all Files"
  3. solution explorer -> select all files and folders -> right-click -> "Include in Project"

In Visual Studio go to the missing files, select them and press del (or right click and select Delete).

Save the project and you are good to go.

As you noted, this is not automatic - the project file needs to be synced up with the actual filesystem. This does not happen with website "projects" because there is no project file.


I've created a PowerShell script to deal with the issue.

function ExtractInclude ($line)
{
    if ($line  -like  '*Content Include=*') {
        return $line.Split('"') | select -Skip 1 | select -First 1
    }
}

function RemoveMissingInclude ([string]$path, [bool]$report) {
    $reader = [System.IO.File]::OpenText($path)
    $projectPath = (Split-Path $path) + "/"

    try {
        for() {
            $line = $reader.ReadLine()
            if ($line -eq $null) { break }

            $pathInclude = ExtractInclude($line)

            if ($report) {
                if ($pathInclude -ne "") {
                    if (-not (Test-Path "$projectPath$pathInclude")) { $pathInclude }
                } 
            } else {
                if ($pathInclude -ne "") {
                    if (Test-Path "$projectPath$pathInclude") { $line }
                } else {
                    $line
                }
           }
        }
    }
    finally {
        $reader.Close()
    }
}

Just run the following to create a cleaned up project file:

RemoveMissingInclude -path "D:\path\name.csproj" | Out-File D:\path\nameClean.csproj

Additional information can be found within this blog post: http://devslice.net/2017/06/remove-missing-references-visual-studio/