public static Task CopyDirectoryAsync(this IsolatedStorageFile Iso, string SourceDirectory, string TargetDirectory, IProgress<double> FractionProgress, bool OverWrite = false) { Contract.Requires(Iso != null); Contract.Requires(FractionProgress != null); Contract.Requires(Iso.DirectoryExists(SourceDirectory), "Source Directory does not exist"); return Task.Factory.StartNew(() => { FractionProgress.Report(0.0); IList<string> relativeFilePaths; IList<string> relativeDirPaths; CollectSubdirectoriesAndFilesBreadthFirst(Iso, SourceDirectory, out relativeDirPaths, out relativeFilePaths); var totalElementCount = relativeDirPaths.Count + //SubDirectories 1 + //TargetDir relativeFilePaths.Count; // Files var reporter = new PercentageReporter<double>(FractionProgress, p => p / 100.0, totalElementCount); var absoluteDirs = from relativeDir in relativeDirPaths select Path.Combine(TargetDirectory, relativeDir); foreach (var dir in Enumerable.Repeat(TargetDirectory, 1).Concat(absoluteDirs)) { if (!Iso.DirectoryExists(dir)) { Iso.CreateDirectory(dir); } reporter.Completed++; } foreach (var relativeFile in relativeFilePaths) { var sourceFile = Path.Combine(SourceDirectory, relativeFile); var targetFile = Path.Combine(TargetDirectory, relativeFile); try { Iso.CopyFile(sourceFile, targetFile, OverWrite); } catch (IsolatedStorageException) { // Ignore } reporter.Completed++; } }); }
private async Task SaveMultimedia(IsolatedStorageFile isoStore, string snapshotDir, IProgress<Tuple<BackupStage, int>> Progress) { Progress.Report(Tuple.Create(BackupStage.ExternalData, 0)); var snapshotMultimediaDir = Path.Combine(snapshotDir, MultimediaStorageService.MEDIA_FOLDER); var snapshotDBPath = Path.Combine(snapshotDir, DiversityDataContext.DB_FILENAME); using (var db = new DiversityDataContext(snapshotDBPath)) { var totalCount = db.MultimediaObjects.Count(); if (totalCount > 0) { var reporter = new PercentageReporter<Tuple<BackupStage, int>>( Progress, p => Tuple.Create(BackupStage.ExternalData, p), totalCount); foreach (var mm in db.MultimediaObjects) { var descriptor = StorageDescriptor.FromURI(mm.Uri); if (descriptor.Type == StorageType.CameraRoll) { using (var content = ImageStore.GetMultimedia(mm.Uri)) { var mmFilePath = Path.Combine(snapshotMultimediaDir, descriptor.FileName); using (var targetFile = isoStore.CreateFile(mmFilePath)) { await content.CopyToAsync(targetFile); descriptor.Type = StorageType.IsolatedStorage; mm.Uri = descriptor.ToString(); } } } reporter.Completed++; } db.SubmitChanges(); } } }