Windows 8 currently does not support an in built app backup to cloud feature as of now. It might be something Microsoft is working on. So for now we will use the alternative approach and use sky drive storage to backup app specific data to cloud.
Why SkyDrive?
- You can a whopping 7 GB cloud storage with a free live account which is way above its competitors.
- Provides seamless integration in windows 8 OS and inside your WinRT application.
How to get started?
- Download the LiveSDK from here
- Add reference to the Live SDK (found under Reference Manager à Windows à Extensions)
- Drop the SignInButton control to your xaml page and name it “signInButton“
- Add two more standard xaml buttons and name them “backupButton” and “restoreButton“
Paste this xaml inside your xaml page:
<Controls:SignInButton x:Name="signInButton" Content="SignInButton" SignInText="Sign In" SignOutText="Sign Out" Width="120" Margin="0, 20, 0, 0" Background="#CBB480" SessionChanged="signInButton_SessionChanged" Scopes="wl.signin wl.basic wl.skydrive_update" />
You can refactor this code as per your need. This will automatically log you in to SkyDrive. It will display the permission page when you run this for the first time so user can grant access to this application to read write user’s SkyDrive folders. The scope property tells what type of access is required by your application.
Scopes="wl.signin wl.basic wl.skydrive_update"
Paste this code inside your xaml.cs page:
private async void LoadData() { try { LiveConnectClient client = new LiveConnectClient(App.Session); LiveOperationResult liveOpResult = await client.GetAsync("me/skydrive/files?filter=folders"); dynamic appResult = liveOpResult.Result; List<object> folderData = appResult.data; foreach (dynamic folder in folderData) { string name = folder.name; if (name == skyDriveFolderName) { skyDriveFolderID = folder.id; } } //Create your App Folder on SkyDrive if does not exist if (string.IsNullOrEmpty(skyDriveFolderID)) { var skyDriveFolderData = new Dictionary<string, object>(); skyDriveFolderData.Add("name", "FolderName"); LiveOperationResult operationResult = await client.PostAsync("me/skydrive", skyDriveFolderData); dynamic result = operationResult.Result; //skyDriveFolderID = Settings.GetSettingsByName("BackupFolderID"); if (string.IsNullOrEmpty(settings.BackupFolderID)) { //Settings.SaveSettings("BackupFolderID", result.id); settings.BackupFolderID = result.id; skyDriveFolderID = result.id; } } else { if (string.IsNullOrEmpty(settings.BackupFolderID)) { //Save to roaming data settings.BackupFolderID = skyDriveFolderID; } } } catch (Exception exception) { this.statusTextBlock.Text = "Error loading data: " + exception.Message; } } private void signInButton_SessionChanged(object sender, Microsoft.Live.Controls.LiveConnectSessionChangedEventArgs e) { ShowHideProgress(); if (e.Status == LiveConnectSessionStatus.Connected) { App.Session = e.Session; this.connectStatusTextBlock.Text = "Signed In! Ready to backup or restore."; backupButton.IsEnabled = true; restoreButton.IsEnabled = true; LoadData(); } ShowHideProgress(); } private async void backupButton_Click(object sender, RoutedEventArgs e) { try { ShowHideProgress(); if (App.Session == null || signInButton.Session == null) { Utilities.ShowMessage("You must Sign in first.", "SkyDrive Connection"); } else { StorageFile backupFile = await ApplicationData.Current.LocalFolder.GetFileAsync("MainSettings.db"); if (backupFile != null && skyDriveFolderID != null) { StorageFile copiedFile = await backupFile.CopyAsync(ApplicationData.Current.LocalFolder, "MainSettings.txt", NameCollisionOption.ReplaceExisting); LiveConnectClient client = new LiveConnectClient(App.Session); LiveOperationResult operationResult = await client.BackgroundUploadAsync(skyDriveFolderID, copiedFile.Name, copiedFile, OverwriteOption.OverWrite); dynamic result = operationResult.Result; settings.BackupFileID = result.id; settings.LastSkyDriveBackup = "Last Backup: " + DateTime.Now.ToString(); this.statusTextBlock.Text = "Last Backup: " + DateTime.Now.ToString(); } else { this.statusTextBlock.Text = "SkyDrive Error: Cannot complete backup."; } } ShowBackupRestoreStatus(); ShowHideProgress(); } catch (LiveConnectException exception) { this.statusTextBlock.Text = "Error during backup: " + exception.Message; } } private async void restoreButton_Click(object sender, RoutedEventArgs e) { try { ShowHideProgress(); if (App.Session == null || signInButton.Session == null) { Utilities.ShowMessage("You must Sign in first.", "SkyDrive Connection"); } else { StorageFile restoreFile = await ApplicationData.Current.LocalFolder.GetFileAsync("MainSettings.txt"); if (restoreFile != null && skyDriveFolderID != null && settings.BackupFileID != null) { LiveConnectClient client = new LiveConnectClient(App.Session); LiveDownloadOperationResult operationResult = await client.BackgroundDownloadAsync(settings.BackupFileID + "/content", restoreFile); if (operationResult.File != null) { var fileStorage = await ApplicationData.Current.LocalFolder.GetFileAsync("MainSettings.txt"); var copiedFileStorage = await fileStorage.CopyAsync(ApplicationData.Current.LocalFolder, "MainSettings.db", NameCollisionOption.ReplaceExisting); this.restoreStatusTextBlock.Text = "Last Restore: " + DateTime.Now; settings.LastRestore = "Last Restore: " + DateTime.Now.ToString(); } } else { this.restoreStatusTextBlock.Text = "No valid backup file to restore, please backup now!"; } } } catch (LiveConnectException exception) { this.restoreStatusTextBlock.Text = "Error during restore: " + exception.Message; } catch (FileNotFoundException) { this.restoreStatusTextBlock.Text = "No valid backup file to restore, please backup now!"; } finally { ShowHideProgress()(); ShowBackupRestoreStatus()(); } } private void ShowBackupRestoreStatus() { return; } private static void ShowHideProgress() { return; }
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles and posts delivered to your feed reader.