コード例 #1
0
        /// <summary>
        /// Copies an entity from a source file system to a destination file system (async with cancellation token).
        /// </summary>
        public async Task CopyAsync(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath, CancellationToken cancellationToken)
        {
            bool isFile = sourcePath.IsFile;

            if (isFile != destinationPath.IsFile)
            {
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");
            }

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                    using (var destinationStream = destination.CreateFile(destinationPath)) {
                        await sourceStream.CopyToAsync(destinationStream, BufferSize, cancellationToken);
                    }
            }
            else
            {
                if (!destinationPath.IsRoot)
                {
                    destination.CreateDirectory(destinationPath);
                }
                foreach (var ep in source.GetEntities(sourcePath))
                {
                    var destinationEntityPath = ep.IsFile ? destinationPath.AppendFile(ep.EntityName) : destinationPath.AppendDirectory(ep.EntityName);
                    await CopyAsync(source, ep, destination, destinationEntityPath, cancellationToken);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Moves an entity from a source file system to a destination file system.
        /// </summary>
        public void Move(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            bool isFile = sourcePath.IsFile;

            if (isFile != destinationPath.IsFile)
            {
                throw new ArgumentException("The specified destination-path is of a different type than the source-path.");
            }

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                    using (var destinationStream = destination.CreateFile(destinationPath)) {
                        sourceStream.CopyTo(destinationStream, BufferSize);
                    }
                source.Delete(sourcePath);
            }
            else
            {
                destination.CreateDirectory(destinationPath);
                foreach (var ep in source.GetEntities(sourcePath).ToArray())
                {
                    var destinationEntityPath = ep.IsFile ? destinationPath.AppendFile(ep.EntityName) : destinationPath.AppendDirectory(ep.EntityName);
                    Move(source, ep, destination, destinationEntityPath);
                }
                if (!sourcePath.IsRoot)
                {
                    source.Delete(sourcePath);
                }
            }
        }
コード例 #3
0
        public void Copy(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination, FileSystemPath destinationPath)
        {
            var pSource          = (PhysicalFileSystem)source;
            var pDestination     = (PhysicalFileSystem)destination;
            var pSourcePath      = pSource.GetPhysicalPath(sourcePath);
            var pDestinationPath = pDestination.GetPhysicalPath(destinationPath);

            if (sourcePath.IsFile)
            {
                System.IO.File.Copy(pSourcePath, pDestinationPath);
            }
            else
            {
                destination.CreateDirectory(destinationPath);
                foreach (var e in source.GetEntities(sourcePath))
                {
                    source.Copy(e, destination, e.IsFile ? destinationPath.AppendFile(e.EntityName) : destinationPath.AppendDirectory(e.EntityName));
                }
            }
        }
コード例 #4
0
        public void Copy(IFileSystem source, FileSystemPath sourcePath, IFileSystem destination,
                         FileSystemPath destinationPath)
        {
            bool isFile;

            if ((isFile = sourcePath.IsFile) != destinationPath.IsFile)
            {
                throw new ArgumentException(
                          "The specified destination-path is of a different type than the source-path.");
            }

            if (isFile)
            {
                using (var sourceStream = source.OpenFile(sourcePath, FileAccess.Read))
                {
                    using (var destinationStream = destination.CreateFile(destinationPath))
                    {
                        var buffer = new byte[BufferSize];
                        int readBytes;
                        while ((readBytes = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            destinationStream.Write(buffer, 0, readBytes);
                        }
                    }
                }
            }
            else
            {
                if (!destinationPath.IsRoot)
                {
                    destination.CreateDirectory(destinationPath);
                }
                foreach (var ep in source.GetEntities(sourcePath))
                {
                    var destinationEntityPath = ep.IsFile
                        ? destinationPath.AppendFile(ep.EntityName)
                        : destinationPath.AppendDirectory(ep.EntityName);
                    Copy(source, ep, destination, destinationEntityPath);
                }
            }
        }