Updating Visual Studio project references from NuGet packages.config
UPDATE: a refined version of this can be found here.
(Among other things), NuGet offers an alternative way of managing project references in Visual Studio.
The usual flow is to right-click on References, and choose “Manage NuGet Packages”. The NuGet dialog then pops up, showing available packages. Once you’ve selected a package, NuGet will:
- download the package to your repository (if required)
- update the VS project file (.csproj or .vbproj etc)
- update its own packages.config file.
What if things get out-of-sync, though, eg you’ve moved your repository so that all the reference hints in the .csproj file are wrong? It’d be nice to be able to use the info in the packages.config to automatically resync the .csproj file.
Here’s one brute-force way to do this (from within the Package Manager console in VS):
get-project -all | %{
$proj = $_ ;
get-package -project $proj.name | %{
uninstall-package -projectname $proj.name -id $_.id -version $_.version -RemoveDependencies -force ;
install-package -projectname $proj.name -id $_.id -version $_.version
}
}
Actually, I have a suspicion there’s a flag for one of the NuGet commands to do this much more simply; but my google-fu failed me on this occasion. If you know of a better way, please comment below.
Posted on January 4, 2012, in Uncategorized and tagged nuget, powershell. Bookmark the permalink. 3 Comments.
Thank you for this! We’re in the middle of moving all of our solutions to a single package directory and I was about to kill myself if I had to manually change all the references.
Brilliant. Many thanks for this.
I created a new project and wanted the same refs as an existing project. So I copied the packages.config across and then by replacing “-all” in the first line of your script with the new project name, I was able update just that one.
Pingback: Updating a NuGet package across all projects in Visual Studio | Dan Haywood