Beispiel #1
0
        /// <summary>
        /// Moves a directory from the source path to the destination path.
        /// </summary>
        /// <param name="srcPath">The source directory.</param>
        /// <param name="dstPath">The destination directory.</param>
        /// <param name="warning">A warning action handler.</param>
        /// <param name="progress">An optional progress action handler.</param>
        /// <returns><b>True</b> if the move was successful, <b>false</b> otherwise.</returns>
        public static bool Move(string srcPath, string dstPath, OperationWarningAction warning, OperationProgressAction progress = null)
        {
            // Validate the arguments.
            if (null == srcPath) throw new ArgumentNullException("srcPath");
            if (null == dstPath) throw new ArgumentNullException("dstPath");
            if (null == warning) throw new ArgumentNullException("warning");

            // Check the source directory exists.
            if (!System.IO.Directory.Exists(srcPath)) return false;
            // If the source path and destination path are the same, return true.
            if (srcPath == dstPath) return true;
            // If the source path is included in the destination path, return false.
            if (dstPath.Contains(srcPath)) return false;

            try
            {
                // If the destination directory does not exist.
                if (!System.IO.Directory.Exists(dstPath))
                {
                    // Try reate the destination directory.
                    System.IO.Directory.CreateDirectory(dstPath);
                }

                // Call the action handler.
                if (null != progress) progress(srcPath, dstPath);

                // Move all files from the source directory to the destination directory.
                foreach (string filePath in System.IO.Directory.EnumerateFiles(srcPath))
                {
                    // Move the file to the destination directory.
                    if (!File.Move(filePath, System.IO.Path.Combine(dstPath, System.IO.Path.GetFileName(filePath)), warning, progress)) return false;
                }

                // For all directories from the source directory.
                foreach (string dirPath in System.IO.Directory.EnumerateDirectories(srcPath))
                {
                    // Move the directory to the destination directory.
                    if (!Directory.Move(dirPath, System.IO.Path.Combine(dstPath, System.IO.Path.GetFileName(dirPath)), warning, progress)) return false;
                }

                // If the directory is empty.
                if (Directory.IsEmpty(srcPath))
                {
                    // Delete the directory.
                    System.IO.Directory.Delete(srcPath, false);
                }
            }
            catch (UnauthorizedAccessException)
            {
                return warning(srcPath, dstPath, Operations.OperationWarning.Unauthorized);
            }
            catch (Exception)
            {
                return warning(srcPath, dstPath, Operations.OperationWarning.Other);
            }

            // Return true.
            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Moves a file from the source path to the destination path.
        /// </summary>
        /// <param name="srcPath">The source file.</param>
        /// <param name="dstPath">The destination file.</param>
        /// <param name="warning">A warning action handler.</param>
        /// <param name="progress">An optional progress action handler.</param>
        /// <returns><b>True</b> if the move was successful, <b>false</b> otherwise.</returns>
        public static bool Move(string srcPath, string dstPath, OperationWarningAction warning, OperationProgressAction progress = null)
        {
            // Validate the arguments.
            if (null == srcPath) throw new ArgumentNullException("srcPath");
            if (null == dstPath) throw new ArgumentNullException("dstPath");
            if (null == warning) throw new ArgumentNullException("warning");

            // Check the source file exists.
            if (!System.IO.File.Exists(srcPath)) return false;
            // If the source path and destination path are the same, return true.
            if (srcPath == dstPath) return true;
            // If the source path is included in the destination path, return false.
            if (dstPath.Contains(srcPath)) return false;

            try
            {
                // Call the action handler.
                if (null != progress) progress(srcPath, dstPath);

                // If the destination file exists.
                if (System.IO.File.Exists(dstPath))
                {
                    // Ask whether the file should be overwritten.
                    if (warning(srcPath, dstPath, Operations.OperationWarning.Exists))
                    {
                        // Delete the destination file.
                        System.IO.File.Delete(dstPath);
                    }
                    else return true; // Otherwise, return true.
                }

                // Move the file.
                System.IO.File.Move(srcPath, dstPath);
            }
            catch (UnauthorizedAccessException)
            {
                return warning(srcPath, dstPath, Operations.OperationWarning.Unauthorized);
            }
            catch (Exception)
            {
                return warning(srcPath, dstPath, Operations.OperationWarning.Other);
            }

            // Return true.
            return true;
        }