2012-02-06 190 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