I have the following code sample (taken from this Gist) working for downloading a NuGet package and its dependencies to a particular folder:
public async Task<HashSet<PackageIdentity>> InstallPackageAndDependenciesAsync(PackageIdentity identity){ bool allowUnlisted = false; bool includePrelease = true; try { ResolutionContext resolutionContext = new ResolutionContext( DependencyBehavior.Lowest, includePrelease, allowUnlisted, VersionConstraints.ExactMajor | VersionConstraints.ExactMinor); INuGetProjectContext projectContext = new BlankProjectContext(Settings.Settings, new NuGetCustomLogger()); var installActions = await LocalNuGetPackageManager.PreviewInstallPackageAsync( LocalNuGetPackageManager.PackagesFolderNuGetProject, identity, resolutionContext, projectContext, new[] { RemoteNuGetFeed }, Array.Empty<SourceRepository>(), CancellationToken.None); var sourceCache = new SourceCacheContext(); await LocalNuGetPackageManager.ExecuteNuGetProjectActionsAsync(LocalNuGetPackageManager.PackagesFolderNuGetProject, installActions, projectContext, sourceCache, CancellationToken.None); return new HashSet<PackageIdentity>(installActions.Select(action => action.PackageIdentity)); } catch (Exception ex) { throw; }}
This works but it downloads the binaries for all supported versions of .NET (.NET standard 2.1, UWP, net45 etc.)
How can I configure it so it will only download package versions appropriate for a specific target framework e.g. .NET 6.0?