2017-02-27 100 views
1

我在Visual Studio上下文中使用NuGet進行包管理。我有一個packages.config文件,並且它可以在我的項目中的一個軟件包目錄下。如何從PowerShell腳本引用NuGet包文件?

NuGet將每個軟件包下載到爲該軟件包命名的目錄及其版本號。我需要在PowerShell中訪問這些包中的文件,但是我想避免在文件路徑中包含版本號,因爲如果我想在packages.config中升級版本,那需要手動更改。

回答

2

我會使用包配置文件

建設的路徑自己
# 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}} 

我想補充的唯一事情是包路徑的唯一性檢查,因爲很有可能你使用的是包在一個以上項目