private async void OnRoamingDataChanged(ApplicationData sender, object args) { using (await f_lock.Acquire()) { var roamingFiles = await Task.WhenAll((await sender.RoamingFolder.GetFilesAsync()) .Where(f => f.Name != Constants.IV_FILE_NAME) .Select(async x => await BindableStorageFile.Create(x))); var newDictionary = (await sender.RoamingFolder.GetFilesAsync()).First(f => f.Name == Constants.IV_FILE_NAME); await newDictionary.CopyAsync(_roamingFolder, Constants.IV_FILE_NAME, NameCollisionOption.ReplaceExisting); //Remove no-longer-synced-files foreach (var file in RoamedFiles) { if (!await ContainsBindableStorageFile(file, roamingFiles)) { await DeleteFileAsync(file.BackingFile, FileLocation.Roamed); } } foreach (var file in roamingFiles.Where(f => f.Name != Constants.IV_FILE_NAME)) { if (!(await ContainsBindableStorageFile(file, RoamedFiles))) { file.IsRoamed = true; await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => RoamedFiles.Add(file)); } } } }
private async Task CreateAsync() { if (_initialized) { return; } ApplicationData.Current.DataChanged += OnRoamingDataChanged; LocalFiles = new ObservableCollection <IBindableStorageFile>(); RoamedFiles = new ObservableCollection <IBindableStorageFile>(); var allFiles = await GetFilesAsync(); foreach (var local in allFiles.LocalFiles) { var bsf = await BindableStorageFile.Create(local); LocalFiles.Add(bsf); } foreach (var roamed in allFiles.RoamingFiles) { if (roamed.Name == Constants.IV_FILE_NAME) { continue; } var bsf = await BindableStorageFile.Create(roamed); bsf.IsRoamed = true; RoamedFiles.Add(bsf); } _initialized = true; IsInitialized.SetResult(true); }
/// <summary> /// Saves and encrypts the contents into a StorageFile with the given name, and the given password. /// </summary> /// <param name="contents">The contents to encrypt.</param> /// <param name="fileName">The filename with which to save the file.</param> /// <param name="password">The password that will be used to generate the encryption key.</param> /// <returns>If successful, returns the created StorageFile.</returns> public async Task <BindableStorageFile> SaveAndEncryptFileAsync(string contents, string fileName, string password) { using (await f_lock.Acquire()) { if (!_initialized) { throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted."); } if (contents == null) { throw new ArgumentException("File contents cannot be null."); } string iv = Guid.NewGuid().ToString("N"); // OLD IV code. string encryptedContents = LegacyEncryptionManager.Encrypt(contents, password, iv); var savedFile = await _localFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName); await FileIO.WriteTextAsync(savedFile, encryptedContents); string savedFileName = savedFile.Name; BindableStorageFile bsf = await BindableStorageFile.Create(savedFile); LocalFiles.Add(bsf); await _ivService.Add(GetParentFolder(bsf.BackingFile), iv, FileLocation.Local); return(bsf); } }