2012-01-10 44 views
12

我有一个很大的解决方案,并且有很多* .cs文件实际上不属于我的解决方案(不包括在csproj文件中)。有什么方法可以找到它们并删除?删除解决方案中未使用的cs文件

+1

你的意思是文件在磁盘上,但没有包含在任何项目中,你想从磁盘上删除它们?或者你的意思是他们包含在你的解决方案中,但是这些类从未使用过? – Ray 2012-01-10 09:32:22

+1

未使用的类可以使用Resharper找到:http://stackoverflow.com/questions/4646174/resharper-find-all-unused-classes但我不确定这是你问的问题。 – Ray 2012-01-10 09:33:35

+2

[Visual Studio宏:查找未包含在该项目中的文件?]的可能重复(http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-在项目中) – Ray 2012-01-10 09:46:04

回答

5

这个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

谢谢!我使用这个脚本来创建一个更详细的脚本,其中包含其他类型的文件,但不使用TFS: https://gist.github.com/mcliment/d9008a9288cea9d088af – 2014-05-14 16:11:39

+3

我也使用这个文件以及@ MarcCliment's来创建另一个PowerShell脚本,它需要一个.sln文件而不是一个proj文件,它会删除所有排除在外的文件项目在提供的解决方案。检查它!http:// g oo.gl/PdR9Fg – mikesigs 2015-01-15 06:44:05

+0

我使用来自@mikesigs的修改过的脚本,其工作方式类似于魅力,只是它错误地将某些文件显示为从WPF应用程序(XAML文件)处于非活动状态。 – Roemer 2016-10-03 06:40:01

6

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

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

+1

是的,我知道这个解决方案,但我的项目非常非常大,所以我正在寻找一些东西来实现这个功能:-( – Nagg 2012-01-10 09:40:19

2

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

鉴于您有一个大型项目,当然不太可能您还没有获得某种源代码控制,因此您可能不得不中断现有连接,在结帐后将原始源位置清除为新的位置,将目标复制到原始位置,并让原件scm检测原件中的文件移除并提交移除。

相关问题