Ejemplo n.º 1
0
 protected void OnCopyErrorOccured(string errorMessage, CopyPair copyPair, Exception ex)
 {
     if (ErrorOccured != null)
     {
         var e = new CopyErrorEventArgs(errorMessage, copyPair, ex);
         ErrorOccured(this, e);
     }
 }
Ejemplo n.º 2
0
        private void ReportError(CopyPair copyPair, Exception exception)
        {
            if (TrowOnError)
            {
                throw new Exception(
                          string.Format("Error copying {0} to {1}. Error: {2}", copyPair.SourceInfo.FullName, copyPair.TargetInfo.FullName, exception.Message), exception);
            }
            var errorMessage = string.Format("Error copying {0} to {1}.", copyPair == null ? "null" : copyPair.SourceInfo.FullName, copyPair == null ? "null" : copyPair.TargetInfo.FullName);

            OnCopyErrorOccured(errorMessage, copyPair, exception);
        }
Ejemplo n.º 3
0
        private void CopyHelper(CopyPair copyPair)
        {
            if (copyPair.IsFile && !copyPair.SourceInfo.Exists)
            {
                throw new FileNotFoundException(
                          string.Format("Source file {0} does not exist.", copyPair.SourceInfo.FullName),
                          copyPair.SourceInfo.FullName);
            }
            else if (!copyPair.IsFile && !copyPair.SourceInfo.Exists)
            {
                throw new DirectoryNotFoundException(string.Format("Source directory {0} does not exist.",
                                                                   copyPair.SourceInfo.FullName));
            }

            // Create parent directory if it does not exist
            var targetDirectory = Path.GetDirectoryName(copyPair.TargetInfo.FullName);

            if (targetDirectory != null && !Directory.Exists(targetDirectory))
            {
                Directory.CreateDirectory(targetDirectory);
            }

            if (copyPair.IsFile)
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Copying file {0} ...", copyPair.SourceInfo.FullName);
                }
                CopyFile((IFileInfo)copyPair.SourceInfo, (IFileInfo)copyPair.TargetInfo);
            }
            else
            {
                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Copying directory {0} ...", copyPair.SourceInfo.FullName);
                }
                CopyDirectory((IDirectoryInfo)copyPair.SourceInfo, (IDirectoryInfo)copyPair.TargetInfo);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Copies file system items overwriting if necessary.
        /// </summary>
        /// <param name="copyPair"><code>CopyPair</code> to copy.</param>
        public void Copy(CopyPair copyPair)
        {
            if (copyPair == null)
            {
                throw new ArgumentNullException("copyPair", "Copy pair is null.");
            }

            try
            {
                Initialize();

                CopyHelper(copyPair);

                CheckStopped();
            }
            catch (StoppedException)
            {
                _log.Debug("FileSystemCopier execution stopped.");
                StopInternal();
            }
            catch (Exception ex)
            {
                var source = "NULL";
                if (copyPair.SourceInfo != null)
                {
                    source = copyPair.SourceInfo.FullName;
                }
                var target = "NULL";
                if (copyPair.TargetInfo != null)
                {
                    target = copyPair.TargetInfo.FullName;
                }
                _log.ErrorFormat("Error occurred while copying {0} to {1}.", source, target);
                ReportError(copyPair, ex);
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Copies file system items overwriting if necessary.
        /// </summary>
        /// <param name="source">Source directory to copy.</param>
        /// <param name="target">Target directory.</param>
        public void Copy(IDirectoryInfo source, IDirectoryInfo target)
        {
            var pair = new CopyPair(source, target);

            Copy(pair);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Copies file system items overwriting if necessary.
        /// </summary>
        /// <param name="source">Source directory to copy.</param>
        /// <param name="target">Target directory.</param>
        public void Copy(DirectoryInfo source, string target)
        {
            var pair = new CopyPair(source, target);

            Copy(pair);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Copies file system items overwriting if necessary.
        /// </summary>
        /// <param name="source">Source file.</param>
        /// <param name="target">Target file.</param>
        public void Copy(IFileInfo source, IFileInfo target)
        {
            var pair = new CopyPair(source, target);

            Copy(pair);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Copies file system items overwriting if necessary.
        /// </summary>
        /// <param name="source">Source file or directory to copy.</param>
        /// <param name="target">Target file path or directory.</param>
        public void Copy(FileInfo source, string target)
        {
            var pair = new CopyPair(source, target);

            Copy(pair);
        }