public static void MoveFile([NotNull] FileInfo file, [NotNull] FileInfo destinationFile, FileMoveOptions fileMoveOptions = FileMoveOptions.ReplaceExisting) { var fileName = file.ArgumentExists().FullName; fileMoveOptions = fileMoveOptions.ArgumentDefined(); for (var retryCount = 0; retryCount < Retries; retryCount++) { try { _ = NativeMethods.MoveFileEx(fileName, destinationFile.FullName, (int)fileMoveOptions); return; } catch (IOException) when(retryCount < Retries - 1) { //RETRY } catch (UnauthorizedAccessException) when(retryCount < Retries - 1) { //RETRY } // If something has a transient lock on the file waiting may resolve the issue Thread.Sleep((retryCount + 1) * 10); } }
public void ArgumentExistsFileInfoTest() { var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify), "tempfileinfotest.dat"); _ = RandomData.GenerateFile(fileName, 500); var tempFile = new FileInfo(fileName); try { tempFile.ArgumentExists(); } catch { Assert.Fail(); } FileInfo nullFileInfo = null; _ = Assert.ThrowsException <NullReferenceException>(() => nullFileInfo.ArgumentExists()); _ = Assert.ThrowsException <FileNotFoundException>(() => new FileInfo("fakefile").ArgumentExists()); tempFile.Delete(); }
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 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 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 void ArgumentExistsFileInfoWithDefaultTest() { var firstFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify), "tempfileinfotest.dat1"); var firstTempFile = new FileInfo(firstFileName); var secondFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.DoNotVerify), "tempfileinfotest.dat2"); _ = RandomData.GenerateFile(secondFileName, 500); var secondTempFile = new FileInfo(secondFileName); try { var result = firstTempFile.ArgumentExists(secondTempFile); Assert.AreEqual(result, secondTempFile); } catch { Assert.Fail(); } secondTempFile.Delete(); }