2012-02-06 72 views
8

我走了VS2010的后面,并删除了一个图像文件夹中的一些图像,该图像文件夹被Web项目引用为内容。在解决方案导航器中,这些文件现在显示黄色警告图标,表明无法找到该文件。刷新文件夹不起作用。有没有办法告诉VS2010自动同步文件夹? VS网站项目默认执行此操作。删除VS2010对Web项目中删除的文件的引用

+1

行为上的差异是由于Web应用程序项目使用基于MSBuild的项目系统来确定包含哪些文件(并在.csproj/.vbproj中列出),而Web站点项目只是查看文件系统。 – Jimmy 2012-02-06 20:17:27

+0

您可以构建项目,然后您将获得缺失文件的列表。根据此列表,您可以检测丢失的文件并将其删除。 – starikovs 2012-07-16 13:01:56

回答

2

在Visual Studio中,找到丢失的文件,选择它们并按del(或右键单击并选择Delete)。

保存该项目,你很好去。

如您所述,这不是自动的 - 项目文件需要与实际文件系统同步。这不会发生在网站“项目”上,因为没有项目文件。

+0

我希望不必这样做,但那就是生活。 – 2012-02-08 06:48:14

+0

我希望不必这样做,但那是活的,不幸的是在新的Visual Studio 2013中。下一次:总是从项目/解决方案中删除,而不是直接在文件资源管理器中... – Langeleppel 2013-12-05 14:16:32

+1

in vs 2015(not确定有关旧版本),项目树上方有一个搜索栏,您可以使用该搜索栏进行过滤,然后多选并点击删除一次。尽管只有在你需要删除的所有文件都被命名为 – agradl 2015-05-07 19:22:03

2

我刚刚在VS 2015中遇到了这个问题。文件在web项目中缺失,所以不想去寻找它们。

回家的捷径是:排除所有文件/文件夹然后将它们包括所有一次。

即:

  1. 解决方案资源管理器 - >选择所有文件和文件夹 - >右键 - > “从计划中排除”
  2. 解决方案资源管理器 - >点击 “显示所有文件”
  3. 解决方案资源管理器 - >选择所有文件和文件夹 - >右键单击 - >“包含在项目中”
+4

时,这才有效。请注意,这种方法的缺点是,您还将包含以前明确排除的文件。 – Chris 2016-05-24 12:59:45

1

我已经创建了一个PowerShell脚本来处理这个问题。

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() 
    } 
} 

只要运行以下命令,创建一个清理项目文件:

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

的更多信息,这篇博客文章中找到:http://devslice.net/2017/06/remove-missing-references-visual-studio/

+0

这很好,但没有考虑到“ Pedro 2017-12-06 19:53:02

-1

我做了一个非常简单的控制台应用程序此:

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