Exemplo n.º 1
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/> or
        ///  <paramref name="targetFilePath"/> is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">The target directory does not exist.</exception>
        /// <exception cref="FileNotFoundException">The file does not exist.</exception>
        public static void CopyFile(IFileSystemEnvironment env, FilePath filePath, FilePath targetFilePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (targetFilePath == null)
            {
                throw new ArgumentNullException(nameof(targetFilePath));
            }

            if (env.FS == null)
            {
                throw new InvalidOperationException(
                          $"Cannot copy file when {nameof(env)}.{nameof(env.FS)} is null");
            }

            var targetDirectoryPath = targetFilePath.GetDirectory().MakeAbsolute(env);

            // Make sure the target directory exist.
            if (!env.FS.Exists(targetDirectoryPath))
            {
                throw new InvalidOperationException($"The directory '{targetDirectoryPath.FullPath}' does not exist.");
            }

            CopyFileCore(env, filePath, targetFilePath);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the relative path to another file.
        /// </summary>
        /// <param name="to">The target file path.</param>
        /// <returns>A <see cref="FilePath"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="to"/> is <see langword="null"/></exception>
        public FilePath GetRelativePath(FilePath to)
        {
            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }

            return(GetRelativePath(to.GetDirectory()).GetFilePath(to.GetFilename()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a file at the specified path or gets an existing file.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <returns>Created or existing file.</returns>
        public static IFile CreateFile(this IFileSystem fileSystem, FilePath path)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            CreateDirectory(fileSystem, path.GetDirectory());

            var file = fileSystem.GetFile(path);

            if (!file.Exists)
            {
                file.OpenWrite().Dispose();
            }

            return(file);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a file at the specified path or gets an existing file. If file was created,
        ///  writes given content to it.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="path">The path.</param>
        /// <param name="contentsBytes">The file contents.</param>
        /// <returns>Created or existing file.</returns>
        public static IFile CreateFile(this IFileSystem fileSystem, FilePath path, byte[] contentsBytes)
        {
            if (fileSystem == null)
            {
                throw new ArgumentNullException(nameof(fileSystem));
            }

            CreateDirectory(fileSystem, path.GetDirectory());

            var file = fileSystem.GetFile(path);

            if (!file.Exists)
            {
                using (var stream = file.OpenWrite()) {
                    using (var ms = new MemoryStream(contentsBytes)) {
                        ms.CopyTo(stream);
                    }
                }
            }

            return(file);
        }
Exemplo n.º 5
0
        /// <exception cref="ArgumentNullException"><paramref name="env"/> or <paramref name="filePath"/> or
        ///  <paramref name="targetFilePath"/>is <see langword="null"/></exception>
        /// <exception cref="InvalidOperationException">The file <paramref name="filePath.FullPath"/> do not exist.
        /// </exception>
        /// <exception cref="FileNotFoundException">The target directory do not exist..</exception>
        public static void MoveFile(IFileSystemEnvironment env, FilePath filePath, FilePath targetFilePath)
        {
            if (env == null)
            {
                throw new ArgumentNullException(nameof(env));
            }
            if (filePath == null)
            {
                throw new ArgumentNullException(nameof(filePath));
            }
            if (targetFilePath == null)
            {
                throw new ArgumentNullException(nameof(targetFilePath));
            }

            filePath       = filePath.MakeAbsolute(env);
            targetFilePath = targetFilePath.MakeAbsolute(env);

            // Make sure the target directory exist.
            var targetDirectoryPath = targetFilePath.GetDirectory().MakeAbsolute(env);

            if (!env.FS.Exists(targetDirectoryPath))
            {
                throw new InvalidOperationException($"The directory '{targetDirectoryPath.FullPath}' do not exist.");
            }

            // Get the file and verify it exist.
            var file = env.FS.GetFile(filePath);

            if (!file.Exists)
            {
                throw new FileNotFoundException($"The file '{filePath.FullPath}' do not exist.", filePath.FullPath);
            }

            // Move the file.
            file.Move(targetFilePath.MakeAbsolute(env));
        }