Example #1
0
        /// <summary>
        /// Saves the source stream to a unified file and adds a comment if the file implrments IVersioningFile.
        /// </summary>
        /// <param name="sourceStream">The source stream.</param>
        /// <param name="targetFile">The target file.</param>
        /// <param name="comment">A comment to add when checking in a versioning file.</param>
        public static void WriteToFile(Stream sourceStream, UnifiedFile targetFile, string comment)
        {
            // It may be a versioning file
            IVersioningFile versioningFile = targetFile as IVersioningFile;

            if (versioningFile != null)
            {
                ThrowIfCheckedOut(versioningFile);
                if (!versioningFile.IsCheckedOut)
                {
                    versioningFile.CheckOut();
                }
            }

            // Copy the source stream to the target file stream.
            using (Stream writeStream = targetFile.Open(FileMode.Create, FileAccess.Write))
            {
                StreamConsumer.CopyToEnd(sourceStream, writeStream);
            }

            // If versioning, then check in with the supplied comment.
            if (versioningFile != null)
            {
                versioningFile.CheckIn(comment ?? String.Empty);
            }
        }