Example #1
0
        /// <summary>
        /// Restore the watch items from OneDrive
        /// </summary>
        /// <returns></returns>
        public async Task <bool> Restore()
        {
            try
            {
                string CompressedFile = "pebble_time_manager_backup.zip";

                //Get backup file
                await OneDrive.DownloadAsync("Backup", CompressedFile);

                Windows.Storage.StorageFolder LocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                Stream FileStream = await LocalFolder.OpenStreamForReadAsync(CompressedFile);

                using (ZipArchive zipArchive = new ZipArchive(FileStream, ZipArchiveMode.Read))
                {
                    foreach (ZipArchiveEntry entry in zipArchive.Entries)
                    {
                        using (Stream entryStream = entry.Open())
                        {
                            byte[] buffer = new byte[entry.Length];
                            entryStream.Read(buffer, 0, buffer.Length);
                            // Create a file to store the contents
                            StorageFile uncompressedFile = await LocalFolder.CreateFileAsync(entry.Name, CreationCollisionOption.ReplaceExisting);

                            // Store the contents
                            using (IRandomAccessStream uncompressedFileStream =
                                       await uncompressedFile.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                using (Stream outstream = uncompressedFileStream.AsStreamForWrite())
                                {
                                    outstream.Write(buffer, 0, buffer.Length);
                                    outstream.Flush();
                                }
                            }

                            System.Diagnostics.Debug.WriteLine("Restore exception: " + entry.Name);
                        }
                    }
                }
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine("Restore exception: " + exp.Message);
                return(false);
            }

            return(true);
        }
Example #2
0
        public async void loadXML()
        {
            Windows.Storage.StorageFolder storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("XML"); // you can get the specific folder from KnownFolders or other folders via FolderPicker as well

            var storageFile = await storageFolder.OpenStreamForReadAsync("FavoriteSuggestionList.xml");

            //Windows.Data.Xml.Dom.XmlLoadSettings loadSettings = new Windows.Data.Xml.Dom.XmlLoadSettings();
            //loadSettings.ProhibitDtd = false; // sample
            //loadSettings.ResolveExternals = false; // sample
            XDocument doc = XDocument.Load(storageFile);

            // XmlDocument doc = await XmlDocument.LoadFromFileAsync(storageFile, loadSettings);
            // XmlNodeList groups = doc.Elements;//.SelectNodes("//FavoriteList/FavoriteItem");

            foreach (var group in doc.Root.Elements("FavoriteItem"))
            {
                SkyDriveFile favoriteItem = new SkyDriveFile((string)group.Attribute("Title"), (string)group.Attribute("URL"), (string)group.Attribute("ImagePath"));
                favoriteItem.IsChecked    = true;
                favoriteItem.ShowCheckBox = true;
                favoriteList.Add(favoriteItem);
            }
        }
        /// <summary>
        /// 从硬盘读取并反序列化账号数据
        /// </summary>
        /// <param name="id"></param>
        private async static Task <int> LoadSerializeAsync(string uid)
        {
            currentaccount = uid;
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            try
            {
                Stream fs = await storageFolder.OpenStreamForReadAsync(currentaccount + ".bat");                                                  //获取本地文件对象的流写入对象

                using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))                    //using语句控制其生命周期
                {
                    DataContractSerializer dcs = new DataContractSerializer(typeof(Dictionary <string, Contacts>));
                    contacts = (Dictionary <string, Contacts>)dcs.ReadObject(reader);                                                          //获取到硬盘中的联系人数据
                }
                return(1);
            }
            catch (Exception e)
            {
                var dialog = new MessageDialog("");
                dialog.Content = "Load Serialize Exception";
                await dialog.ShowAsync();

                return(0);
            }
        }