Beispiel #1
0
        /// <summary>
        /// Creates a copy of the srcFile in the specified path
        ///     This method also specifies what to do if a srcFile with the same name already exists.
        /// </summary>
        /// <param name="destPath">
        /// The destination path where the copy of the srcFile is created.
        /// </param>
        /// <param name="nameCollisionOption">
        /// One of the <see cref="Enum"/> values that determines how to handle the collision if a srcFile
        ///     with the specified "<param name="destPath"/>" already exists.
        /// </param>
        /// <returns>
        /// <see cref="FileInfo"/> that represents the copy
        ///     of the srcFile that path of it is "<param name="destPath">".
        /// </returns>
        public static FileInfo CopyTo(this FileInfo srcFile, string destPath,
                                      NameCollisionOption nameCollisionOption)
        {
            if (string.IsNullOrWhiteSpace(destPath))
            {
                throw new ArgumentNullException(nameof(destPath));
            }

            switch (nameCollisionOption)
            {
            case NameCollisionOption.GenerateUniqueName:
                var item = FileSystemInfoEx.GetFileSystemInfo(destPath);
                if (item.Exists)
                {
                    return(srcFile.CopyTo(item.GetParent().FullName,
                                          ProductNextName(srcFile.GetPureName()) + srcFile.Extension,
                                          nameCollisionOption));
                }
                else
                {
                    return(srcFile.CopyTo(destPath));
                }

            case NameCollisionOption.ReplaceExisting:
                return(srcFile.CopyTo(destPath, true));

            case NameCollisionOption.FailIfExists:
                return(srcFile.CopyTo(destPath, false));

            default:
                throw new ArgumentException("invalid option", nameof(nameCollisionOption));
            }
        }
        public static DirectoryInfo CopyTo(this DirectoryInfo srcDir, string destPath,
                                           NameCollisionOption option = NameCollisionOption.FailIfExists)
        {
            if (string.IsNullOrWhiteSpace(destPath))
            {
                throw new ArgumentNullException(nameof(destPath));
            }
            if (!srcDir.Exists)
            {
                throw new DirectoryNotFoundException("Source folder is not exist");
            }
            var item = FileSystemInfoEx.GetFileSystemInfo(destPath);

            if (item.Exists)
            {
                if (!string.Equals(srcDir.FullName, item.FullName, StringComparison.OrdinalIgnoreCase))
                {
                    switch (option)
                    {
                    case NameCollisionOption.GenerateUniqueName:
                        return(srcDir.CopyTo(item.GetParent().FullName, ProductNextName(item.Name), option));

                    case NameCollisionOption.ReplaceExisting:
                        item.Delete();
                        break;

                    case NameCollisionOption.FailIfExists:
                        var type = item is DirectoryInfo ? "srcDir" : "file";
                        throw new IOException($"A {type} exists in {destPath}");

                    default:
                        throw new ArgumentException("invalid option", nameof(option));
                    }
                }
                else
                {
                    throw new IOException("Source and destination folders are same");
                }
            }

            var destFolder = new DirectoryInfo(destPath);

            destFolder.Create();
            var files = srcDir.EnumerateFiles();

            foreach (var file in files)
            {
                file.CopyTo(destFolder);
            }
            var subFolders = srcDir.EnumerateDirectories();

            foreach (var subFolder in subFolders)
            {
                subFolder.CopyTo(destFolder);
            }
            return(destFolder);
        }
Beispiel #3
0
        /// <summary>
        /// Moves the current file to <see cref="destinationFullPath"/>.
        /// This method also specifies what to do if a file with the
        ///     same name already exists in the specified folder.
        /// </summary>
        /// <param name="destPath">
        /// new full path of file, including name and extension.
        /// </param>
        /// <param name="option">
        /// An <see cref="Enum"/> value that determines how responds if a file with same path
        /// is exist in the destination folder.
        /// </param>
        public static void MoveTo(this FileInfo file, string destPath,
                                  NameCollisionOption nameCollisionOption)
        {
            if (string.IsNullOrWhiteSpace(destPath))
            {
                throw new ArgumentNullException(nameof(destPath));
            }
            if (!file.Exists)
            {
                // like this, we will have a real exception XD. (FileNotFoundException)
                file.MoveTo(destPath);
            }

            var item = FileSystemInfoEx.GetFileSystemInfo(destPath);

            if (item.Exists)
            {
                if (!string.Equals(file.FullName, item.FullName, StringComparison.OrdinalIgnoreCase))
                {
                    switch (nameCollisionOption)
                    {
                    case NameCollisionOption.GenerateUniqueName:
                        file.MoveTo(item.GetParent(),
                                    ProductNextName(file.GetPureName()) + file.Extension, nameCollisionOption);
                        return;

                    case NameCollisionOption.ReplaceExisting:
                        item.Delete();     //No problem. Existence of source checked in start of method.
                        break;

                    case NameCollisionOption.FailIfExists:
                        // System.IO default option. .net will throw exception
                        break;

                    default:
                        throw new ArgumentException("invalid option", nameof(nameCollisionOption));
                    }
                }
            }
            file.MoveTo(destPath);
        }
        public static void Move(this DirectoryInfo srcDir, string destPath,
                                NameCollisionOption nameCollisionOption)
        {
            if (string.IsNullOrWhiteSpace(destPath))
            {
                throw new ArgumentNullException(nameof(destPath));
            }
            if (!srcDir.Exists)
            {
                throw new DirectoryNotFoundException("Source folder is not exist");
            }
            var item = FileSystemInfoEx.GetFileSystemInfo(destPath);

            if (item.Exists)
            {
                if (!string.Equals(srcDir.FullName, item.FullName, StringComparison.OrdinalIgnoreCase))
                {
                    switch (nameCollisionOption)
                    {
                    case NameCollisionOption.GenerateUniqueName:
                        srcDir.Move(item.GetParent().FullName,
                                    ProductNextName(item.Name), nameCollisionOption);
                        return;

                    case NameCollisionOption.ReplaceExisting:
                        item.Delete();
                        break;

                    case NameCollisionOption.FailIfExists:
                        var type = item is DirectoryInfo ? "srcDir" : "file";
                        throw new IOException($"A {type} exists in {destPath}");

                    default:
                        throw new ArgumentException("invalid option", nameof(nameCollisionOption));
                    }
                }
                else
                {
                    if (string.Equals(srcDir.FullName, item.FullName, StringComparison.Ordinal))
                    {
                        throw new IOException("Source and destination folders are same");
                    }
                    else
                    {
                        srcDir.Move(item.GetParent(), ProductNextName(srcDir.Name),
                                    NameCollisionOption.GenerateUniqueName);
                        srcDir.Move(destPath, nameCollisionOption);
                        return;
                    }
                }
            }

            try
            {
                srcDir.MoveTo(destPath);
            }
            catch (IOException)
            {
                srcDir.CopyTo(destPath, nameCollisionOption);
                srcDir.Delete();
            }
        }