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.

About these ads

Posted on January 4, 2012, in Uncategorized and tagged , . Bookmark the permalink. 3 Comments.

  1. 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.

  2. 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.

  1. Pingback: Updating a NuGet package across all projects in Visual Studio | Dan Haywood

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 197 other followers

%d bloggers like this: