private async Task Continue(FilePickTargets target,
                                    IStorageFile read, IStorageFile write = null)
        {
            switch (target)
            {
            case FilePickTargets.Databases:
                await _registration.RegisterAsync(read);

                break;

            case FilePickTargets.KeyFile:
                var view = _rootFrame.Content as FrameworkElement;
                if (view == null)
                {
                    break;
                }

                var viewModel = view.DataContext as PasswordViewModel;
                if (viewModel != null)
                {
                    await viewModel.AddKeyFile(read);
                }
                break;

            case FilePickTargets.Attachments:
                CachedFileManager.DeferUpdates(write);
                await read.CopyAndReplaceAsync(write);

                await CachedFileManager.CompleteUpdatesAsync(write);

                break;
            }
        }
        private async void ExportBtn_Click(object sender, RoutedEventArgs e)
        {
            IStorageFile ExFile = await AppStorage.SaveFileAsync("Text File", new string[] { ".log" });

            if (ExFile == null)
            {
                return;
            }
            await CurrentFile.CopyAndReplaceAsync(ExFile);
        }
        public async Task <bool> WriteFile(IStorageFile file, IStorageFile from)
        {
            try
            {
                await from.CopyAndReplaceAsync(file);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemple #4
0
        public async Task ExportAttachment(SignalAttachment sa)
        {
            try
            {
                Logger.LogTrace("ExportAttachment() locking");
                await SemaphoreSlim.WaitAsync(CancelSource.Token);

                Logger.LogTrace("ExportAttachment() locked");
                var savePicker = new Windows.Storage.Pickers.FileSavePicker
                {
                    SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Downloads,
                    SuggestedFileName      = sa.SentFileName ?? "signal"
                };
                savePicker.FileTypeChoices.Add("Any", new List <string>()
                {
                    "."
                });
                var target_file = await savePicker.PickSaveFileAsync();

                if (target_file != null)
                {
                    CachedFileManager.DeferUpdates(target_file);
                    IStorageFile localCopy = await ApplicationData.Current.LocalCacheFolder.GetFileAsync($@"Attachments\{sa.Id}.plain");

                    await localCopy.CopyAndReplaceAsync(target_file);

                    Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(target_file);
                }
            }
            catch (Exception e)
            {
                Logger.LogError("ExportAttachment failed: {0}\n{1}", e.Message, e.StackTrace);
            }
            finally
            {
                SemaphoreSlim.Release();
                Logger.LogTrace("ExportAttachment() released");
            }
        }
 /// <summary>
 /// Asynchronously stomps the content of this candidate with the contents
 /// of the specified IStorageFile.
 /// </summary>
 /// <param name="file">The file with which to replace this data.</param>
 /// <returns>A Task representing the operation.</returns>
 public Task ReplaceWithAsync(IStorageFile file)
 {
     return(file.CopyAndReplaceAsync(this.candidate.AsIStorageFile).AsTask());
 }
        private async Task Continue(FilePickTargets target,
            IStorageFile read, IStorageFile write = null)
        {
            switch (target)
            {
                case FilePickTargets.Databases:
                    await _registration.RegisterAsync(read);
                    break;

                case FilePickTargets.KeyFile:
                    var view = _rootFrame.Content as FrameworkElement;
                    if (view == null)
                        break;

                    var viewModel = view.DataContext as PasswordViewModel;
                    if (viewModel != null)
                        await viewModel.AddKeyFile(read);
                    break;

                case FilePickTargets.Attachments:
                    CachedFileManager.DeferUpdates(write);
                    await read.CopyAndReplaceAsync(write);
                    await CachedFileManager.CompleteUpdatesAsync(write);
                    break;
            }
        }
Exemple #7
0
        private static async Task <bool> PullRoamingData(FileSyncInfo info)
        {
            var romingFolder = ApplicationData.Current.RoamingFolder;
            var folder       = ApplicationData.Current.LocalFolder;

            // ローカル側のファイルを取得
            // LocalFolderからfileまでの相対的なフォルダ構造を再現
            IStorageFile localFile           = null;
            bool         isNotExistLocalFile = false;
            {
                var folderPathStack = info.RelativeFilePath.Split('/', '\\').ToList();
                var parentFolder    = folder;
                foreach (var folderName in folderPathStack.Take(folderPathStack.Count - 1 /* without file name */))
                {
                    parentFolder = await parentFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
                }

                var fileName = folderPathStack.Last();
                localFile = await parentFolder.TryGetItemAsync(fileName) as IStorageFile;

                if (localFile == null)
                {
                    localFile = await parentFolder.CreateFileAsync(fileName);

                    isNotExistLocalFile = true;
                }

                if (localFile == null)
                {
                    throw new Exception();
                }
            }

            IStorageFile roamingFile = null;

            {
                var folderPathStack = info.RelativeFilePath.Split('/', '\\').ToList();
                var parentFolder    = romingFolder;
                foreach (var folderName in folderPathStack.Take(folderPathStack.Count - 1 /* without file name */))
                {
                    parentFolder = await parentFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);
                }

                var fileName = folderPathStack.Last();
                roamingFile = await parentFolder.TryGetItemAsync(fileName) as IStorageFile;

                if (roamingFile == null)
                {
                    Debug.WriteLine(info.RelativeFilePath + " はローミングフォルダに存在しません。");
                    return(false);
                }
            }



            if (isNotExistLocalFile)
            {
                await roamingFile.CopyAndReplaceAsync(localFile);

                Debug.WriteLine(localFile.Path + "をローカルにコピー");
            }
            else
            {
                // ローカル側ファイルとinfo.UpdateAtを比較して
                // ローカル側ファイルが古い場合
                // ローミングファイルをローカル側ファイルに上書きコピー
                var localProp = await localFile.GetBasicPropertiesAsync();

                if (info.UpdateAt > localProp.DateModified)
                {
                    await roamingFile.CopyAndReplaceAsync(localFile);

                    Debug.WriteLine(localFile.Path + "をローカルにコピー(上書き)");
                }
            }

            return(true);
        }