Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 2010 删除解决方案中未使用的cs文件_Visual Studio 2010_C# - Fatal编程技术网

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

Visual studio 2010 删除解决方案中未使用的cs文件,visual-studio-2010,c#,Visual Studio 2010,C#,我有一个大的解决方案,有许多*.cs文件实际上不再属于我的解决方案(不包括在csproj文件中)。有没有办法找到所有文件并将其删除?在解决方案资源管理器中选择项目时,单击解决方案资源管理器工具栏中的“显示所有文件”按钮。这将显示项目目录中的文件和文件夹,但不包括在项目中。这允许您删除它们或将它们读取到项目中 我不知道有什么自动化的解决方案,所以您必须为每个项目手动执行此操作。此PowerShell脚本应该满足您的要求。它解析项目文件以获取包含的代码文件。然后将该列表与磁盘上的实际文件进行比较。剩

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

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


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

此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
    }
}

[CmdletBinding()]
param(
[参数(位置=0,强制=true)]
[string]$Project,
[参数(必需=$false)]
[ValidateRange(10,12)]
[int]$VsVersion=12,
[开关]$DeleteFromDisk,
[开关]$TfsCheckin
)
$ErrorActionPreference=“停止”
$tfPath=“${env:ProgramFiles(X86)}\Microsoft Visual Studio$VsVersion.0\Common7\IDE\TF.exe”
$projectPath=分割路径$project
if($Project.EndsWith(“csproj”))
{
$fileType=“*.cs”
}
其他的
{
$fileType=“*.vb”
}
$fileType

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


考虑到您有一个大型项目,您当然不可能还没有某种源代码管理,因此您可能必须断开现有连接,在签出到新位置后清除原始源位置,将目标复制到原件,让原件scm检测到原件中的文件删除并提交删除

你是说这些文件在磁盘上,但不包括在任何项目中,你想从磁盘上删除它们吗?或者你的意思是,它们包含在你的解决方案中,但从未使用过的类?可以使用Resharper找到未使用的类:但我不确定你是否在问这个问题。这里还讨论了另一种方法的可能重复:是的,我知道该解决方案,但我的项目非常大,因此我正在寻找一些东西来自动化这一点:-(谢谢!我使用此脚本创建了一个更详细的脚本,其中包括其他类型的文件,而不使用TFS:我也使用此文件和@Marcclient创建了另一个PowerShell脚本,该脚本采用.sln文件而不是单个proj文件。它删除了所提供解决方案中所有项目中排除的所有文件。请检查!我告诉我们e来自@mikesigs的修改后的脚本,除了它错误地将WPF应用程序中的一些文件(XAML文件)显示为非活动文件外,工作起来很有魅力。