Exemple #1
0
        /// <summary>
        /// The destinationSubfolder parameter is separated from destinationFolder because
        /// all obsolete files in the destinationFolder must be deleted.
        /// </summary>
        public void AddFolderContent(string sourceFolder, string destinationFolder, string destinationSubfolder, bool recursive)
        {
            sourceFolder      = Path.GetFullPath(sourceFolder).TrimEnd(new[] { '\\' });
            destinationFolder = Path.GetFullPath(destinationFolder).TrimEnd(new[] { '\\' });

            string invalidFolder = _filesByDestination.Keys.FirstOrDefault(existingDestination => existingDestination != destinationFolder &&
                                                                           (existingDestination.StartsWith(destinationFolder) || destinationFolder.StartsWith(existingDestination)));

            if (invalidFolder != null)
            {
                throw new ArgumentException(GetType().Name + " cannot be used on two destination folders where one contains another: \""
                                            + invalidFolder + "\" and \"" + destinationFolder + "\".");
            }

            if (Directory.Exists(sourceFolder))
            {
                foreach (var file in Directory.GetFiles(sourceFolder, "*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                {
                    _filesByDestination.Add(destinationFolder, new CopyFile
                    {
                        File   = file,
                        Target = Path.Combine(destinationFolder, destinationSubfolder, file.Substring(sourceFolder.Length + 1))
                    });
                }
            }
            else
            {
                _filesByDestination.AddKey(destinationFolder);
            }
        }