/// <summary>Copies the contents of a stream to a file, asynchronously.</summary>
        /// <param name="source">The source stream.</param>
        /// <param name="destinationPath">The path to the destination file.</param>
        /// <returns>A Task that represents the asynchronous operation.</returns>
        public static Task CopyStreamToFileAsync(this Stream source, string destinationPath)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destinationPath == null)
            {
                throw new ArgumentNullException("destinationPath");
            }

            // Open the output file for writing
            var destinationStream = FileAsync.OpenWrite(destinationPath);

            // Copy the source to the destination stream, then close the output file.
            return(CopyStreamToStreamAsync(source, destinationStream).ContinueWith(t =>
            {
                var e = t.Exception;
                destinationStream.Close();
                if (e != null)
                {
                    throw e;
                }
            }, TaskContinuationOptions.ExecuteSynchronously));
        }
Пример #2
0
        /// <summary>Copies the contents of a stream to a file, asynchronously.</summary>
        /// <param name="source">The source stream.</param>
        /// <param name="destinationPath">The path to the destination file.</param>
        /// <returns>A Task that represents the asynchronous operation.</returns>
        public static Task CopyStreamToFileAsync(this Stream source, string destinationPath)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (destinationPath == null)
            {
                throw new ArgumentNullException("destinationPath");
            }
            FileStream destinationStream = FileAsync.OpenWrite(destinationPath);

            return(source.CopyStreamToStreamAsync(destinationStream).ContinueWith(delegate(Task t) {
                AggregateException exception = t.Exception;
                destinationStream.Close();
                if (exception != null)
                {
                    throw exception;
                }
            }, TaskContinuationOptions.ExecuteSynchronously));
        }