繁体   English   中英

从.csproj文件中将所有Nuget依赖项作为DLL提取到指定目录

[英]Fetch all Nuget dependencies from .csproj file as DLLs to a specified directory

有什么方法可以将.csproj文件中的所有Nuget依赖项作为DLL提取到指定目录?

有什么方法可以将.csproj文件中的所有Nuget依赖项作为DLL提取到指定目录?

是否要基于.csproj文件将dll文件提取到指定目录? 如果是,则NuGet不支持此功能,但是您可以使用其他技术来满足此要求,例如Power Shell。

为此,请使用以下代码在项目文件目录下创建Power Shell脚本GetDllFiles.ps1

$projectFiles = get-childitem . *.csproj -Recurse 

foreach( $projectFile in $projectFiles )
{
    $projectXml = [xml] (get-content $projectFile.FullName)
    $projectDir = $projectFile.DirectoryName

    Write-Host "# $($projectFile.FullName) #"


    foreach( $itemGroup in $projectXml.Project.ItemGroup )
    {
        if( $itemGroup.Reference.Count -eq 0 )
        {
            continue
        }

        foreach( $reference in $itemGroup.Reference )
        {
            if( $reference.Include -eq $null )
            {
                continue
            }

            if( $reference.HintPath -eq $null )
            {
                Write-Host ("{0}" -f $reference.Include)
            }
            else
            {
                $fullpath = $reference.HintPath
                if(-not [System.IO.Path]::IsPathRooted( $fullpath ) )
                {
                    $fullPath = (join-path $projectDir $fullpath)
                    $fullPath = [System.IO.Path]::GetFullPath("$fullPath")
                }
                Write-Host $fullPath
                Copy-Item $fullPath D:\Test
            }
        }
    }

    Write-Host ''
    Start-Sleep –s 10
}

该Power Shell脚本会抓取当前目录下的每个.csproj文件,并检查每个Reference。 对于从GAC引用的程序集,仅输出名称。 对于GAC外部的程序集,输出程序集的完整路径,然后可以将其复制到指定目录。

更新评论:

但是我想要一点不同的事情-从.csproj获取所有NuGet程序包,然后将它们“解包”到指定的目录中,以便获得我的应用程序依赖的所有DLL。

如果要将整个nuget软件包而不是dll文件都提取到指定目录,则可以使用简单的nuget命令行来实现:

nuget restore "xx.csproj" -OutputDirectory "SpecifiedDirectoryPath"

此命令行将获取整个nuget软件包并将其解压缩到指定目录中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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