删除解决方案中未使用的cs文件

时间:2012-01-10 09:29:24

标签: visual-studio-2010 c#

我有一个很大的解决方案,并且有许多* .cs文件实际上不属于我的解决方案(不包括在csproj文件中)。有没有办法找到所有这些并删除?

3 个答案:

答案 0 :(得分:7)

此PowerShell脚本应该可以满足您的需求。它解析项目文件以获取包含的代码文件。然后它将该列表与磁盘上的实际文件进行比较。其余文件是未使用/过时的文件。

脚本可以从磁盘中删除未使用的文件,也可以将它们作为删除在TFS中删除。

<#
.SYNOPSIS
Find and process files in a project folder that are not included in the project. 


.DESCRIPTION
Find and process files in a project folder that are not included in the project. 
Options to delete the files or to add them as pending deletes for TFS. Use TF.exe to pend the deletes and start the check-in process for the files.
This is necessary when trying to delete files that are not currently included in a Visual Studio project.

.PARAMETER Project
The path/name for the project file. 

.PARAMETER VsVersion
The Visual Studio version (10, 11, 12). Used to locate the tf.exe file.  

.PARAMETER DeleteFromDisk
Just delete the files from disk. No interaction with any source control.

.PARAMETER TfsCheckin
After pending the deletes, open the check-in dialog.

#>

[CmdletBinding()]
param(
    [Parameter(Position=0, Mandatory=$true)]
    [string]$Project,  
    [Parameter(Mandatory=$false)]
    [ValidateRange(10,12)] 
    [int] $VsVersion = 12,
    [switch]$DeleteFromDisk,
    [switch]$TfsCheckin
)

$ErrorActionPreference = "Stop"
$tfPath = "${env:ProgramFiles(X86)}\Microsoft Visual Studio $VsVersion.0\Common7\IDE\TF.exe"

$projectPath = Split-Path $project


if($Project.EndsWith("csproj"))
{
    $fileType = "*.cs"
}
else
{
    $fileType = "*.vb"
}
$fileType


$projectFiles = Select-String -Path $project -Pattern '<compile'  | % { $_.Line -split '\t' } | `
     % {$_ -replace "(<Compile Include=|\s|/>|["">])", ""} | % { "{0}\{1}" -f $projectPath, $_ }
Write-Host "Project files:" $projectFiles.Count


$diskFiles = gci -Path $path -Recurse -Filter $fileType | % { $_.FullName}
Write-Host "Disk files:" $diskFiles.Count


$diff = (compare-object $diskFiles $projectFiles  -PassThru) 
Write-Host "Excluded Files:" $diff.Count

#create a text file for log purposes
$diffFilePath = Join-Path $projectPath "DiffFileList.txt" 
$diff | Out-File $diffFilePath  -Encoding UTF8
notepad $diffFilePath


#just remove the files from disk
if($DeleteFileOnly)
{
    $diff | % { Remove-Item -Path $_ -Force -Verbose}
}
else #TFS options
{
    #this will add the files as pending deletes in TFS (awaiting check-in)
    $diff | % {
        [Array]$arguments = @("delete", "`"$_`"")
        & "$tfPath" $arguments
    }

    if($Checkin)
    {
        #start the check-in process for the pending deletes
        [Array]$arguments = "checkin", "/recursive", "$projectPath"
        & $tfPath $arguments
    }
}

答案 1 :(得分:6)

在解决方案资源管理器中选择项目时,单击解决方案资源管理器工具栏中的“显示所有文件”按钮。这将显示项目目录中的文件和文件夹,但不包含在项目中。这允许您删除它们或将它们读入项目。

我不知道自动化解决方案,因此您必须手动为每个项目执行此操作。

答案 2 :(得分:2)

使用visual studio将所有文件添加到源代码管理中。它只会添加属于项目一部分的文件,因此不会添加非项目文件。然后,您可以简单地提交所有文件并在其他地方检查项目。只会在目标位置签出相关文件。

鉴于你有一个大项目,你当然不可能没有某种源控制,所以你可能必须打破现有连接,在结账后清除原来的源位置到新位置,将目标复制到原始文件并让原始文件scm检测原文中的文件删除并提交删除。