我有一个很大的解决方案,并且有很多* .cs文件实际上不属于我的解决方案(不包括在csproj文件中)。有什么方法可以找到它们并删除?删除解决方案中未使用的cs文件
回答
这个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
}
}
谢谢!我使用这个脚本来创建一个更详细的脚本,其中包含其他类型的文件,但不使用TFS: https://gist.github.com/mcliment/d9008a9288cea9d088af – 2014-05-14 16:11:39
我也使用这个文件以及@ MarcCliment's来创建另一个PowerShell脚本,它需要一个.sln文件而不是一个proj文件,它会删除所有排除在外的文件项目在提供的解决方案。检查它!http:// g oo.gl/PdR9Fg – mikesigs 2015-01-15 06:44:05
我使用来自@mikesigs的修改过的脚本,其工作方式类似于魅力,只是它错误地将某些文件显示为从WPF应用程序(XAML文件)处于非活动状态。 – Roemer 2016-10-03 06:40:01
当您在解决方案资源管理器中选择项目时,单击解决方案资源管理器工具栏上的“显示所有文件”按钮。这将显示项目目录中的文件和文件夹,但未包含在项目中。这允许您删除它们或将它们读取到项目中。
我不知道自动化解决方案,所以你必须为每个项目手动执行此操作。
是的,我知道这个解决方案,但我的项目非常非常大,所以我正在寻找一些东西来实现这个功能:-( – Nagg 2012-01-10 09:40:19
使用visual studio将所有文件添加到源代码管理。它只会添加作为项目一部分的文件,因此不会添加非项目文件。然后,您可以简单地提交所有文件,并在其他地方检查项目。只有相关文件将在目标位置检出。
鉴于您有一个大型项目,当然不太可能您还没有获得某种源代码控制,因此您可能不得不中断现有连接,在结帐后将原始源位置清除为新的位置,将目标复制到原始位置,并让原件scm检测原件中的文件移除并提交移除。
- 1. 从.net解决方案中删除未使用的DLL
- 2. 删除解决方案后功能未被删除
- 3. VS2010 - 删除不在解决方案中的文件
- 4. 从TFS中删除解决方案?
- 5. Riak-cs解决方案与防病毒
- 6. Visual Studio从解决方案文件中删除TFS DefaultCollection路径
- 7. 用于从PDF文件中删除文本的C#解决方案
- 8. EF Core - 删除列解决方案(sqlite)
- 9. 删除NuGet包从解决方案
- 10. 如何使用Roslyn从单个.cs文件创建解决方案?
- 11. 收回Sharepoint解决方案后文件不会被删除
- 12. msbuild解决方案文件
- 13. 使用EAV表的“软删除”解决方案有问题吗?
- 14. 解决方案文件夹外部解决方案目录
- 15. 重构解决方案文件夹到新解决方案
- 16. 为VS2008解决方案创建VS2010解决方案文件
- 17. VS 2008中的解决方案文件
- 18. 如何使用CRM SDK删除解决方案?
- 19. 使用angularjs组件的解决方案
- 20. 使用gwt的插件解决方案
- 21. 从一批文件中删除项目的更优雅的解决方案?
- 22. VS2012未检测到解决方案中的文件夹
- 23. 解决方案A中的TFS显示解决方案B的未决更改 - 使用团队基础服务
- 24. 在解决方案中查找文件
- 25. 从解决方案中的所有枚举中删除空值
- 26. 从Expression Blend 4中的解决方案中删除项目?
- 27. 解决方案使用MATLAB
- 28. 方法链解决方案在pandas中删除列级别DataFrame
- 29. 通用的解决方案要删除的不作为外键
- 30. 在codeplex中删除我的解决方案的一个项目
你的意思是文件在磁盘上,但没有包含在任何项目中,你想从磁盘上删除它们?或者你的意思是他们包含在你的解决方案中,但是这些类从未使用过? – Ray 2012-01-10 09:32:22
未使用的类可以使用Resharper找到:http://stackoverflow.com/questions/4646174/resharper-find-all-unused-classes但我不确定这是你问的问题。 – Ray 2012-01-10 09:33:35
[Visual Studio宏:查找未包含在该项目中的文件?]的可能重复(http://stackoverflow.com/questions/2000197/visual-studio-macro-find-files-that-arent-included-在项目中) – Ray 2012-01-10 09:46:04