Esempio n. 1
0
        public static async Task<SyncHelper> LoadSyncSettings()
        {
            SyncHelper syncSettings = new SyncHelper();

            StorageFile settingsFile = null;
            bool isExists = true;

            try
            {
                settingsFile = await StorageFile.GetFileFromPathAsync(AppConstants.SyncSettingsFile);
            }
            catch (FileNotFoundException)
            {
                isExists = false;
            }

            if (!isExists)
            {
                return syncSettings;
            }
            else
            {
                using (var fileStream = await settingsFile.OpenStreamForReadAsync())
                {
                    MemoryStream memoryStream = new MemoryStream();
                    await fileStream.CopyToAsync(memoryStream);
                    memoryStream.Position = 0;

                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SyncHelper));
                    syncSettings = (SyncHelper)serializer.ReadObject(memoryStream);
                    return syncSettings;
                }
            } 
        }
Esempio n. 2
0
        public static async void SaveSyncSettings(SyncHelper syncSettings)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SyncHelper));

            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile settingsFile = await folder.CreateFileAsync("settings.json", CreationCollisionOption.ReplaceExisting);

            using (var fileStream = await settingsFile.OpenStreamForWriteAsync())
            {
                serializer.WriteObject(fileStream, syncSettings);
            }
        }