我知道我可以从 PM 控制台卸载包。我遇到了另一个项目的一些依赖问题,我想重新开始,我需要一次性删除所有包。有办法吗?
9 回答
To get all packages from all projects in the solution use Get-Package
. To get all packages from a specific project use Get-Package -ProjectName "YourProjectName"
.
Remove all packages from all projects in the solution
Be careful: This will uninstall ALL packages in the solution. If
-Force
parameter is used, packages are removed even if dependencies exist.
Get-Package | Uninstall-Package -RemoveDependencies -Force
Remove all packages from a specific project within a solution
Be careful: This will uninstall ALL packages in the project. If
-Force
parameter is used, packages are removed even if dependencies exist.
Get-Package -ProjectName "YourProjectName" |
Uninstall-Package -ProjectName "YourProjectName" -RemoveDependencies -Force
在包管理器控制台中,只需键入:
获取包 | 卸载包-删除依赖项
尝试这个:
get-package | uninstall-package -removedependencies -force
如果要从解决方案中的每个项目中卸载每个 NuGet 包,请在 NuGet 包管理器控制台中使用它:
foreach($project in $projects){ $packages = Get-Package -ProjectName $project.Name; foreach($package in $packages){ Uninstall-Package -ProjectName $project.Name -Id $package.Id -Force} }
在我的例子中使用 -Force 参数会留下项目文件修改和对正常卸载包时应该删除的一些二进制文件的引用。
这是一种无需使用 -Force 参数即可从特定项目中卸载所有包的简单方法。实际上,它会一遍又一遍地尝试卸载包,直到没有包剩下,所以你会看到一些提到依赖包的错误(如果你有的话),但是随着叶包在每次迭代中被删除,它们会越来越少地出现。
另外值得一提的是,我只在 PackageManager 控制台中测试了以下 PowerShell 片段。(“工具 > NuGet 包管理器 > 包管理器控制台”)
从解决方案中的所有项目中卸载所有包
while((Get-Project -All | Get-Package).Length -gt 0) { Get-Project -All | Get-Package | Uninstall-Package }
仅删除包含单词“ WildCardSearch ”的项目
while((Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*' | Get-Package).Length -gt 0) { Get-Project -All | Where-Object ProjectName -like '*WildCardSearch*' | Get-Package | Uninstall-Package }
请注意,如果除了依赖包之外还有其他问题阻止卸载包,则此代码段将永远运行,直到您手动停止它。
我不相信这是可能的,所以一次卸载所有软件包。但是,正如您已经指出的那样,您可以卸载软件包,但您也可以告诉它卸载其依赖项,执行以下操作:
卸载包 OpenIdPortableArea –RemoveDependencies
这是 Marcus Hammarberg 的博客对此进行了解释:http: //www.marcusoft.net/2011/02/nuget-uninstall-remove-dependencies.html
更新了脚本以删除 VS 解决方案中单个项目的所有 nuget 包:
$projectName = "MyProjectName"; $packages = Get-Package -ProjectName $projectName; foreach($package in $packages){ Uninstall-Package -ProjectName $projectName -Id $package.Id -Force}
使用包管理器控制台自动恢复(卸载和安装而不更新到最新版本)包:
更新包-重新安装
...或仅针对特定项目:
更新包-重新安装-项目项目名称
虚拟老式 for 循环:
$packages = get-package
$packageId = "Apache.NMS.ActiveMQ"
$counter = 1
foreach($package in $packages){
if($package.Id -eq $packageId)
{
Write-Host "`n$counter-Deleting Package:`n"
$package
Uninstall-Package -Id $packageId -ProjectName $package.ProjectName -RemoveDependencies
Write-Host "`n============================================`n"
$counter++
}
}