简体   繁体   中英

How can I reference NuGet package files from a PowerShell script?

I'm using NuGet for package management outside the context of Visual Studio. I have a packages.config file, and can nuget restore it to a packages directory in my project.

NuGet downloads each package to a directory named for the package plus its version number. I need to access files from these packages in PowerShell, but I want to avoid including the version number in the file path, since that would require a manual change if I want to advance versions in packages.config.

I would build the path myself using the packages config files

# Get solution folder
$PathToSolution = "C:\Some\Path\SolutionFolder"

# find all packages under the solution folder
$configs = gci $PathToSolution -Filter packages.config -recurse

# get content of every package config file
$configs.fullname | %{[xml]$packages = Get-Content $_;
                  $packages.packages.package | foreach{
                                               # Get pacakge attributes
                                               $packageId=$_.id;
                                               $packageVersion=$_.version;
                                               # Build the path from the build attributes
                                               $Path="$PathToSolution\packages\$packageId.$PackageVersion";
                                               Write-output $Path}}

The only thing i would add is check of uniqueness of the package path, because most likely you're using some packages in more then one project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM