コード例 #1
0
        public static void DownloadPackage(string packageId, IVersion version, Uri feedUri, string targetFilePath)
        {
            if (!Directory.Exists(feedUri.LocalPath))
            {
                throw new Exception($"Path does not exist: '{feedUri}'");
            }

            // Lookup files which start with the name "<Id>." and attempt to match it with all possible version string combinations (e.g. 1.2.0, 1.2.0.0)
            // before opening the package. To avoid creating file name strings, we attempt to specifically match everything after the last path separator
            // which would be the file name and extension.
            var package = (from path in GetPackageLookupPaths(packageId, version, feedUri)
                           let p = new LocalNuGetPackage(path)
                                   where p.Metadata.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) &&
                                   VersionFactory.CreateVersion(p.Metadata.Version.ToString(), PackageMetadataFactory.GetMetadataFromPackageID(packageId).VersionFormat).Equals(version)
                                   select p).FirstOrDefault();

            if (package == null)
            {
                throw new Exception($"Could not find package {packageId} {version} in feed: '{feedUri}'");
            }

            Log.VerboseFormat("Found package {0} version {1}", package.Metadata.Id, package.Metadata.Version);
            Log.Verbose("Downloading to: " + targetFilePath);

            using (var targetFile = File.Open(targetFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                package.GetStream(stream =>
                {
                    stream.CopyTo(targetFile);
                });
            }
        }