使用C#代码下载.nupkg 包
代码:
1 public static async Task DownloadAsync(string packageId, string version) 2 { 3 var package = new PackageIdentity(packageId, NuGetVersion.Parse(version)); 4 5 var settings = Settings.LoadDefaultSettings(null); 6 var globalFolder = SettingsUtility.GetGlobalPackagesFolder(settings); 7 8 var logger = NullLogger.Instance; 9 var cancelToken = CancellationToken.None; 10 11 var sourceRepositoryProvider = new SourceRepositoryProvider(settings, Repository.Provider.GetCoreV3()); 12 var downloadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Download"); 13 using (var cacheContext = new SourceCacheContext()) 14 { 15 var downloadContext = new PackageDownloadContext(cacheContext, downloadFolder, true); 16 17 var repositories = sourceRepositoryProvider.GetRepositories(); 18 foreach (var repository in repositories) 19 { 20 var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync(repository, 21 package, 22 downloadContext, 23 globalFolder, 24 logger, 25 cancelToken); 26 27 if (downloadResult != null) 28 { 29 var downloadFile = $@" {downloadFolder} \ {package.Id} . {package.Version} .nupkg"; 30 31 using (var fileStream = File.Create(downloadFile)) 32 { 33 downloadResult.PackageStream.Seek(0, SeekOrigin.Begin); 34 await downloadResult.PackageStream.CopyToAsync(fileStream); 35 } 36 37 break; 38 } 39 } 40 } 41 }