10
votes

I went behind VS2010's back and deleted some images from an image folder that's referenced by a web project as Content. In the solution navigator, these files now show up with the yellow warning icon that the file cannot be found. Refreshing the folder has no effect. Is there a way to tell VS2010 to automatically synch a folder? The VS Website project does this by default.

6
The difference in behavior is due to the Web Application project uses the MSBuild based project system to determine what files are included (and listed in the .csproj/.vbproj), whereas the Web Site project just looks at the files system. - Jimmy
You can build the project and then you will get the list of missing files. Based on this list you are able to detect missing files and remove them. - starikovs

6 Answers

5
votes

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.

3
votes

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"
3
votes

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/

0
votes

Answering in Nov-2018 as some body like me might be facing this issue.

Problem:

I have backed up some folders in the project.

Due to files are replicas of original files, the function definitions were referenced.

And now when I try to open the function definition from function call, the one from back up folder is opening.

After deleting the back up folders, it is showing me error: xyz.php file not found, create one.

Solution:

Go to the folder in Explorer,

Click on Refresh icon.

Restart the Visual Studio Code.

0
votes

open project in Explorer and find file with extension '.vcxproj' and remove the file you want to remove from and you are good to go

-1
votes

I made a very simple console app for this:

using System;
using System.IO;
using System.Collections.Generic;

namespace CleanProject
{
    class Program
    {
        static void Main(string[] args)
        {
            var newFile = new List<string>();
            if (args.Length == 0)
            {
                Console.WriteLine("Please specify the project full path as an argument");
                return;
            }

            var projFile = args[0];
            if (!File.Exists(projFile))
            {
                Console.WriteLine("The specified project file does not exist: {0}", projFile);
                return;
            }

            if (!projFile.ToLowerInvariant().EndsWith(".csproj"))
            {
                Console.WriteLine("The specified does not seem to be a project file: {0}", projFile);
                return;
            }

            Console.WriteLine("Started removing missing files from project:", projFile);

            var newProjFile = Path.Combine(Path.GetDirectoryName(projFile), Path.GetFileNameWithoutExtension(projFile) + ".Clean.csproj");
            var lines = File.ReadAllLines(projFile);
            var projectPath = Path.GetDirectoryName(projFile);
            for(var i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                if (!line.Contains("<Content Include=\"") && !line.Contains("<None Include=\""))
                {
                    newFile.Add(line);
                }
                else
                {
                    var start = line.IndexOf("Include=\"") + "Include=\"".Length;
                    var end = line.LastIndexOf("\"");
                    var path = line.Substring(start, end - start);
                    if (File.Exists(Path.Combine(projectPath, path)))
                    {
                        newFile.Add(line);
                    }
                    else
                    {
                        if (!line.EndsWith("/>")) // I'm assuming it's only one line inside the tag
                            i += 2;
                    }
                }
            }
            File.WriteAllLines(newProjFile, newFile);

            Console.WriteLine("Finished removing missing files from project.");
            Console.WriteLine("Cleaned project file: {0}", newProjFile);
        }
    }
}

https://github.com/woodp/remove-missing-project-files