示例#1
0
        /// <summary>
        /// Set a different <see cref="System.Uri"/> to the same file targeted by this instance of <see cref="NetworkFileStream"/>
        /// </summary>
        /// <param name="uriToSameFile">New <see cref="System.Uri"/> host must match existing host.</param>
        public void SetUriForSameFile(Uri uriToSameFile)
        {
            ArgumentValidator.EnsureNotNullOrWhiteSpace(uriToSameFile?.AbsoluteUri, nameof(uriToSameFile));

            if (uriToSameFile.Host != Uri.Host)
            {
                throw new ArgumentException($"New uri to the same file must have the same host.\r\n Old Host :{Uri.Host}\r\nNew Host: {uriToSameFile.Host}");
            }
            if (hasBegunDownloading && !finishedDownloading)
            {
                throw new Exception("Cannot change Uri during a download operation.");
            }

            Uri         = uriToSameFile;
            HttpRequest = WebRequest.CreateHttp(Uri);

            HttpRequest.CookieContainer = CookieContainer;
            HttpRequest.Headers         = RequestHeaders;
            //If NetworkFileStream is resuming, Header will already contain a range.
            HttpRequest.Headers.Remove("Range");
            HttpRequest.AddRange(WritePosition);
        }