/// <summary> /// Перемещение содержимого директории /// </summary> /// <param name="sourceDirectory">начальная директория</param> /// <param name="targetDirectory">Конечная директория</param> /// <param name="cancellationToken">Токен отмены</param> public static void MoveAllContents(string sourceDirectory, string targetDirectory, CancellationToken?cancellationToken = null) { ThrowHelper.CheckNotNullOrWhiteSpace(sourceDirectory, nameof(sourceDirectory)); ThrowHelper.CheckNotNullOrWhiteSpace(targetDirectory, nameof(targetDirectory)); ThrowHelper.CheckTrue(PathHelper.GetPathTargetType(sourceDirectory) == PathTargetTypes.Directory, "Path '" + sourceDirectory + "' is not a directory."); PathTargetTypes pathTargetType = PathHelper.GetPathTargetType(targetDirectory); ThrowHelper.CheckTrue(pathTargetType == PathTargetTypes.Directory || pathTargetType == PathTargetTypes.Unknown, "'" + targetDirectory + "' should be a directory or should not exist."); if (sourceDirectory != targetDirectory) { CreateDirectoryIfNotExists(targetDirectory); string[] directories = Directory.GetDirectories(sourceDirectory); foreach (string str in directories) { cancellationToken?.ThrowIfCancellationRequested(); MoveAllContents(str, Path.Combine(targetDirectory, str)); } string[] files = Directory.GetFiles(sourceDirectory); foreach (string str2 in files) { cancellationToken?.ThrowIfCancellationRequested(); string fileName = Path.Combine(sourceDirectory, str2); string targetPath = Path.Combine(targetDirectory, str2); FileInfo file = new FileInfo(fileName); StreamHelper.WriteToFileSafely(file.OpenReadLocked(), targetPath); File.Delete(fileName); } } }
/// <summary> /// Получение типа объета файловой системы по пути /// </summary> /// <param name="path">Путь</param> /// <returns></returns> public static PathTargetTypes GetPathTargetType(string path) { ThrowIfInvalid(path); bool exists = new FileInfo(path).Exists; PathTargetTypes pathTargetType = exists || new DirectoryInfo(path).Exists ? (exists ? PathTargetTypes.File : PathTargetTypes.Directory) : PathTargetTypes.Unknown; return(pathTargetType); }