views:

170

answers:

4

As our projects have evolved over the years many .cs files have been removed from projects, renamed etc. but they remain in source control. So when you pull latest code you get all these .cs files that are no longer referenced in any .csproj file.

Anyone know an easy, reliable mechanism for detecting these files and eliminating them?

A: 

It's best to use a scripting language for this task. I find that powershell is well suited for situations like this.

Step 1 is to find all of the actual .cs files that are inlcuded in your csproj files. The following function will search a directory structure for all csproj files and return the set of .cs files that are included in those files

function Get-IncludedCsFiles() {
  param ( $rootPath = $(throw "Need a path")
  $projFiles = gci -re -in *.csproj $rootPath
  foreach ( $file in $projFile ) { 
    $dir = split-path $file.FullName
    foreach ( $line in (gc $file) ) { 
      if ( $line -match 'Compile\s+Include="([^"]+)"' ) {
        join-path $dir $matches[1]
      }
    }
  }
}

Now all you need to do is wrap that into a dictionary and then do a search in the directory structure

$map = @{}
$pathToSearch = "Path\To\Search"
Get-IncludedCsFiles $pathToSearch | %{ $map[$_] = $true }
$notIncluded = gci -re -in *.cs $pathToSearch |
               ?{ -not $map.Contains($file.FullName) }
JaredPar
+1  A: 

I don't have VS on hand at the moment, but isn't there a "Show Files Not in Project" option in the solution explorer ?

jfclavette
It is "Show All Files" on the Solution Explorer Toolbar.
Richard
+4  A: 

Try "Show All Files" in VS solution explorer.

Partha Choudhury
A: 

One way that you can avoid this problem is procedural: (works for any project except for a "Web Site" project, which has no project file):

Instead of doing a "get latest" from your source control tool, always do an "open from source control" inside of Visual Studio, and do a get latest from Solution Explorer. VS will only pull files that belong to the project.

It's not a perfect answer -- when you perform merges, you will likely end up pulling down every file in your branch, but it has worked very well for us.

(of course, this requires that you are using a source control tool that offers VS integration -- it also requires that you add any non-compiled, but required files (icons, etc) to your project, or they will not be pulled down by VS).

JMarsch