/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is LocalDirectoryInfo)) throw new ArgumentException("The target directory must be of type LocalDirectoryInfo.", "targetDirectory"); try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { string targetFilePath = this.CombinePath(targetDirectory.FullName, sourceFile.Name); try { using (FileStream targetStream = File.Create(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 256 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } catch (IOException) { File.Delete(targetFilePath); throw; } } } catch (UnauthorizedAccessException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (SecurityException ex) { throw new AccessException("The file could not be accessed.", ex); } catch (IOException ex) { throw new AccessException("The file could not be accessed.", ex); } }
private void LoadToTempFile() { string path = Path.GetTempFileName(); using (Stream sourceStream = File.OpenRead(this.OriginalPath)) { using (Stream targetStream = File.OpenWrite(path)) { var operation = new StreamCopyOperation(sourceStream, targetStream, 32 * 1024, true); operation.CopyProgressChanged += (sender, e) => this.OnCachingProgressChanged(e); operation.Execute(); } } this.StreamingPath = path; }
/// <summary> /// Copies the specified file to the target directory. /// </summary> /// <param name="sourceFileSystem">The source file system.</param> /// <param name="sourceFile">The source file.</param> /// <param name="targetDirectory">The target directory.</param> /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception> /// /// <exception cref="ArgumentException">The target directory is not of type <see cref="FlagFtp.FtpDirectoryInfo"/>.</exception> public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory) { sourceFileSystem.ThrowIfNull(() => sourceFileSystem); sourceFile.ThrowIfNull(() => sourceFile); targetDirectory.ThrowIfNull(() => targetDirectory); if (!(targetDirectory is FtpDirectoryInfo)) throw new ArgumentException("The target directory must be of type FtpDirectoryInfo.", "targetDirectory"); Uri targetFilePath = new Uri(this.CombinePath(targetDirectory.FullName, sourceFile.Name)); try { using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile)) { using (Stream targetStream = this.client.OpenWrite(targetFilePath)) { if (sourceFile.Length > 0) { var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 8 * 1024, true); copyOperation.CopyProgressChanged += (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e); copyOperation.Execute(); } } } } catch (WebException ex) { switch (ex.Status) { case WebExceptionStatus.ConnectFailure: case WebExceptionStatus.ProxyNameResolutionFailure: throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex); } throw new AccessException("The file could not be accessed", ex); } }