public static async Task <long> CopyFileAsync([NotNull] FileInfo file, [NotNull] DirectoryInfo destination) { var fileName = file.ArgumentExists().FullName; _ = destination.ArgumentNotNull().CheckExists(throwException: true, createDirectory: true, errorMessage: string.Format(CultureInfo.InvariantCulture, Resources.DirectoryDoesNotExistOrCannotBeCreated, destination.FullName)); var destinationName = destination.FullName; var newFileName = Path.Combine(destinationName, fileName); using (FileStream sourceStream = File.Open(fileName, FileMode.Open)) { if (File.Exists(newFileName)) { File.Delete(newFileName); } using (FileStream destinationStream = File.Create(newFileName)) { await sourceStream.CopyToAsync(destinationStream).ConfigureAwait(false); await destinationStream.FlushAsync().ConfigureAwait(false); } } return(file.Length); }
public static long CopyFile([NotNull] FileInfo file, [NotNull] DirectoryInfo destination) { var fileName = file.ArgumentExists().FullName; if (destination.ArgumentNotNull().CheckExists(throwException: true)) { var destinationName = destination.FullName; var newFileName = Path.Combine(destinationName, fileName); using (FileStream sourceStream = file.Open(FileMode.Open)) { if (File.Exists(newFileName)) { File.Delete(newFileName); } using FileStream destinationStream = File.Create(newFileName); sourceStream.CopyTo(destinationStream); destinationStream.Flush(); } return(file.Length); } else { return(NoResult); } }
public static void MoveDirectory([NotNull] DirectoryInfo source, [NotNull] DirectoryInfo destination, int retries = 10) { source = source.ArgumentExists(); retries = retries.ArgumentInRange(1, upper: 100, defaultValue: 10, errorMessage: Resources.RetriesAreLimitedTo0100); if (destination.ArgumentNotNull().CheckExists(throwException: true)) { var tries = 0; do { tries++; if (tries > 1) { // If something has a transient lock on the file waiting may resolve the issue Thread.Sleep((retries + 1) * 10); } try { Directory.Move(source.FullName, destination.FullName); return; } catch (IOException) when(tries >= retries) { throw; } catch (UnauthorizedAccessException) when(tries >= retries) { throw; } }while (tries < retries); } }
public static long GetSize([NotNull] this DirectoryInfo path, string searchPattern = "*.*", SearchOption searchOption = SearchOption.TopDirectoryOnly) { path = path.ArgumentNotNull(); searchPattern = searchPattern.ArgumentNotNullOrEmpty(); searchOption = searchOption.ArgumentDefined(); return(path.GetFiles(searchPattern, searchOption).Sum(p => p.Length)); }
public static async Task UnZipAsync([NotNull] FileInfo file, [NotNull] DirectoryInfo destination) { var fileName = file.ArgumentExists().FullName; _ = destination.ArgumentNotNull().CheckExists(); var destinationPath = destination.FullName; await UnWinZipAsync(fileName, destinationPath).ConfigureAwait(false); }
public static async Task DownloadFileFromWebAndUnzipAsync([NotNull] Uri remoteUri, [NotNull] DirectoryInfo destination) { _ = destination.ArgumentNotNull().CheckExists(); var tempDownloadPath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}{Path.GetExtension(remoteUri.ToString())}"); await DownloadFileFromWebAsync(remoteUri, destination).ConfigureAwait(false); await UnZipAsync(new FileInfo(tempDownloadPath), destination, true).ConfigureAwait(false); }
public static async Task UnZipAsync([NotNull] FileInfo file, [NotNull] DirectoryInfo destination, bool deleteZipFile) { file = file.ArgumentExists(); _ = destination.ArgumentNotNull().CheckExists(); await UnZipAsync(file, destination).ConfigureAwait(false); if (deleteZipFile) { file.Delete(); } }
public static async Task UnGZipAsync([NotNull] FileInfo source, DirectoryInfo destination) { source = source.ArgumentExists(); if (destination.ArgumentNotNull().Exists is false) { destination.Create(); } var destinationPath = destination.FullName; using (FileStream gzipStream = source.OpenRead()) { using (var expandedStream = new GZipStream(gzipStream, CompressionMode.Decompress)) { using (FileStream targetFileStream = File.OpenWrite(destinationPath)) { await expandedStream.CopyToAsync(targetFileStream).ConfigureAwait(false); } } } }
public static async Task DownloadFileFromWebAsync(Uri remoteUri, DirectoryInfo destination) { remoteUri = remoteUri.ArgumentNotNull(); if (destination.ArgumentNotNull().Exists is false) { destination.Create(); } var pathName = destination.FullName; using (HttpClient client = GetHttpClient()) { using (FileStream localStream = File.Create(pathName)) { using (Stream stream = await client.GetStreamAsync(remoteUri).ConfigureAwait(false)) { await stream.CopyToAsync(localStream).ConfigureAwait(false); } await localStream.FlushAsync().ConfigureAwait(false); } } }
public static void CopyDirectory([NotNull] DirectoryInfo source, [NotNull] DirectoryInfo destination, bool overwrite = true) { DirectoryInfo[] directories = source.ArgumentExists().GetDirectories(); destination.ArgumentNotNull().CheckExists(); var destinationPath = destination.FullName; FileInfo[] files = source.GetFiles(); for (var fileIndex = 0; fileIndex < files.Length; fileIndex++) { FileInfo file = files[fileIndex]; _ = file.CopyTo(Path.Combine(destinationPath, file.Name), overwrite); } for (var directoryIndex = 0; directoryIndex < directories.Length; directoryIndex++) { DirectoryInfo subDirectory = directories[directoryIndex]; CopyDirectory(subDirectory, new DirectoryInfo(Path.Combine(destinationPath, subDirectory.Name)), overwrite); } }
public int CopyFiles([NotNull] IEnumerable <FileInfo> files, [NotNull] DirectoryInfo destination) { FileInfo[] list = files.ArgumentNotNull().ToArray(); _ = destination.ArgumentNotNull().CheckExists(); var destinationPath = destination.FullName; var successCount = 0; for (var fileCount = 0; fileCount < list.Length; fileCount++) { FileInfo tempFile = list[fileCount]; if (tempFile.Exists) { try { var newFileName = new FileInfo(fileName: tempFile.FullName.Replace(tempFile.Directory.Root.FullName, destinationPath, StringComparison.InvariantCulture)); if (newFileName.Directory.Exists is false) { newFileName.Directory.Create(); } var psw = PerformanceStopwatch.StartNew(); _ = tempFile.CopyTo(newFileName.FullName, overwrite: true); TimeSpan perf = psw.StopReset(); successCount += 1; this.OnProcessed(new FileProgressEventArgs { Name = tempFile.FullName, Message = tempFile.Name, ProgressState = FileProgressState.Copied, Size = tempFile.Length, SpeedInMilliseconds = perf.TotalMilliseconds, }); } catch (Exception ex) { // Send error. this.OnProcessed(new FileProgressEventArgs { Name = tempFile.FullName, ProgressState = FileProgressState.Error, Size = tempFile.Length, Message = ex.Message, }); } } else { this.OnProcessed(new FileProgressEventArgs { Name = tempFile.FullName, ProgressState = FileProgressState.Error, Size = tempFile.Length, Message = Resources.FileNotFound, }); } } return(successCount); }
public static void ThrowDirectoryNotFoundException(string message, [NotNull] DirectoryInfo directory) { throw new DirectoryNotFoundException(message.DefaultIfNull(Resources.ErrorDirectoryNotFound), directory.ArgumentNotNull()); }