Ejemplo n.º 1
0
        /// <summary>
        /// Generates a unique name for a folder.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="name">The name.</param>
        /// <param name="iterations">The maximum iterations. Defaults to 99.</param>
        /// <returns>A new unique name for the folder.</returns>
        public virtual string GenerateFolderUniqueName(AFolder directory, string name, int iterations = 99)
        {
            string basename = Path.GetFileNameWithoutExtension(name);
            string ext = Path.GetExtension(name);
            StringBuilder str = new StringBuilder();

            for (int i = 0; i < iterations; i++)
            {
                switch (i)
                {
                    case 0:
                        if (!FolderExists(Path.Combine(directory.ToString(), name)))
                            return name;
                        break;

                    default:
                        str.Clear();
                        str.Append(String.Format("{0} - ({1})", basename, i));
                        if (!FolderExists(Path.Combine(directory.ToString(), str.ToString())))
                            return str.ToString();
                        break;
                }
            }

            throw Exceptions.CanNotGenerateUniqueNameException(iterations);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Moves the System.IO.File.
        /// </summary>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="destinationName">Name of the destination.</param>
        /// <param name="collisionOption">The collision option. Defaults to FailIfExists.</param>
        /// <returns>Whether the file was moved or not.</returns>
        public override bool Move(AFolder destinationPath, string destinationName, FileCollisionOption collisionOption = FileCollisionOption.FailIfExists)
        {
            if (!IsOpen)
            {
                Exceptions.NotNullCheck<AFolder>(destinationPath, nameof(destinationPath));
                Exceptions.NotNullOrEmptyException(destinationName, nameof(destinationName));

                string newPath = System.IO.Path.Combine(destinationPath.ToString(), destinationName);
                switch (collisionOption)
                {
                    case FileCollisionOption.FailIfExists:
                        if (Fenrir.FileSystem.FileExists(newPath))
                            return false;
                        break;

                    case FileCollisionOption.GenerateUniqueName:
                        newPath = Fenrir.FileSystem.GenerateFileUniqueName(newPath);
                        break;

                    case FileCollisionOption.ReplaceExisting:
                        if (Fenrir.FileSystem.FileExists(newPath))
                            System.IO.File.Delete(newPath);
                        break;
                }

                System.IO.File.Move(FullPath, newPath);
                SetupFile(newPath);
                return true;
            }

            return false;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a folder.
        /// </summary>
        /// <param name="directory">The directory.</param>
        /// <param name="folder">The folder.</param>
        /// <param name="openMode">The open mode. Defaults to Normal.</param>
        /// <returns>The folder.</returns>
        public override AFolder OpenFolder(AFolder directory, string folder, OpenMode openMode)
        {
            string path = FSHelpers.CombinePath(directory.ToString(), folder);

            if (!Fenrir.FileSystem.FolderExists(path))
            {
                switch (openMode)
                {
                    case OpenMode.Normal:
                        return Fenrir.FileSystem.CreateFolder(path, FileCollisionOption.FailIfExists);

                    case OpenMode.FailIfDoesNotExist:
                        throw new FileNotFoundException(String.Format("Folder {0} does not exist!", path));
                }
            }

            return new FenrirFolder(path);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Copies the System.IO.File.
        /// </summary>
        /// <param name="destinationPath">The destination path.</param>
        /// <param name="destinationName">Name of the destination.</param>
        /// <param name="collisionOption">The collision option. Defaults to FailIfExists.</param>
        /// <returns>An AFile representing the copied System.IO.File.</returns>
        public override AFile Copy(AFolder destinationPath, string destinationName, FileCollisionOption collisionOption = FileCollisionOption.FailIfExists)
        {
            if (!IsOpen)
            {
                Exceptions.NotNullCheck<AFolder>(destinationPath, nameof(destinationPath));
                Exceptions.NotNullOrEmptyCheck(destinationName, nameof(destinationName));

                string destination = System.IO.Path.Combine(destinationPath.ToString(), destinationName);

                switch (collisionOption)
                {
                    case FileCollisionOption.FailIfExists:
                        if (Fenrir.FileSystem.FileExists(destination))
                            return null;
                        break;

                    case FileCollisionOption.GenerateUniqueName:
                        destination = Fenrir.FileSystem.GenerateFileUniqueName(destination);
                        break;
                }

                System.IO.File.Copy(FullPath, destination, collisionOption == FileCollisionOption.ReplaceExisting);
                return new FenrirFile(destination);
            }

            return null;
        }