Blog Archives

Restore NuGet packages from packages.config

Another in my occasional series of wrangling with NuGet within Visual Studio.

This one is to restore all packages from a packages.config file:

function Restore-Packages() {
  $proj = get-project
  get-package -project $proj.name | % {
    Write-Host $_.id;
    uninstall-package -projectname $proj.name -id $_.id -version $_.version -RemoveDependencies -force ;
    install-package -projectname $proj.name -id $_.id -version $_.version
  }
}

To use, first edit the packages.config as you require. Then, in the Package Manager Console in VS (in the ‘default project’ dropdown set correctly), just type:

Restore-Packages

Enjoy.

Updating a NuGet package across all projects in Visual Studio

A little while back I did a couple of posts how to force a reinstall of all references in a Visual Studio from the NuGet packages.config.

A similar requirement is to update a package to its latest version, across all projects. Here’s how:

Read the rest of this entry

Updating Visual Studio project references from NuGet packages.config (2)

Here’s a small refinement to the script I wrote a while back to update VS project references from the packages.config file:

function Sync-References([string]$PackageId) {
  get-project -all | %{
    $proj = $_ ;
    Write-Host $proj.name; 
    get-package -project $proj.name | ? { $_.id -match $PackageId } | % { 
      Write-Host $_.id; 
      uninstall-package -projectname $proj.name -id $_.id -version $_.version -RemoveDependencies -force ;
      install-package -projectname $proj.name -id $_.id -version $_.version
    }
  }
}

You can then use it to update selected packages, eg:

Sync-References FluentAssertions

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):

Read the rest of this entry

Follow

Get every new post delivered to your Inbox.

Join 197 other followers

%d bloggers like this: