/// <summary> /// Load the accounts from the accounts file /// </summary> private async Task LoadAccountsAsync() { string accountFileName = GetAccountFileName(); FileInfo fileInfo = new FileInfo(accountFileName); if (!fileInfo.Exists || fileInfo.Length == 0) { using (File.Create(accountFileName)) { } await this.SaveAccountsAsync().ConfigureAwait(false); } //Try first to deserialize using the new account deserialization mechanism, if that fails, fall back to the old way bool writeBackToFile = false; try { XmlSerializer x = new XmlSerializer(typeof(DefaultAccountSerializationContainer)); using (StreamReader reader = new StreamReader(accountFileName)) { object deserializedObject = x.Deserialize(reader); //TODO: This is not technically async... DefaultAccountSerializationContainer accountContainer = (DefaultAccountSerializationContainer)deserializedObject; List <Account> accounts = accountContainer.GetAccountCollection(this); //Update accounts to the new URI format if required writeBackToFile = this.UpdateAccountsToNewUriFormat(accounts); this.Accounts = new ObservableCollection <Account>(accounts); } } catch (InvalidOperationException) { writeBackToFile = true; XmlSerializer x = new XmlSerializer(typeof(LegacyAccountManager)); using (StreamReader reader = new StreamReader(accountFileName)) { object deserializedObject = x.Deserialize(reader); //TODO: This is not technically async... LegacyAccountManager legacyAccountManager = (LegacyAccountManager)deserializedObject; this.Accounts = new ObservableCollection <Account>(legacyAccountManager.CreateAccounts(this)); } } //One time write back to change format of the file if (writeBackToFile) { await this.SaveAccountsAsync().ConfigureAwait(false); } }
/// <summary> /// Save the accounts to the account file /// </summary> private async Task SaveAccountsAsync() { string fileName = GetAccountFileName(); IEnumerable <DefaultAccount> defaultAccountList = this.Accounts.Cast <DefaultAccount>(); DefaultAccountSerializationContainer serializationContainer = new DefaultAccountSerializationContainer(defaultAccountList); XmlSerializer x = new XmlSerializer(typeof(DefaultAccountSerializationContainer)); using (var writer = new StreamWriter(fileName, false)) { x.Serialize(writer, serializationContainer); await writer.FlushAsync().ConfigureAwait(false); } }
/// <summary> /// Load the accounts from the accounts file /// </summary> private async Task LoadAccountsAsync() { string accountFileName = GetAccountFileName(); if (!File.Exists(accountFileName)) { using (var myFile = File.Create(accountFileName)) { } await this.SaveAccountsAsync().ConfigureAwait(false); } //Try first to deserialize using the new account deserialization mechanism, if that fails, fall back to the old way bool initialLoadFailed = false; try { XmlSerializer x = new XmlSerializer(typeof(DefaultAccountSerializationContainer)); using (StreamReader reader = new StreamReader(accountFileName)) { object deserializedObject = x.Deserialize(reader); //TODO: This is not technically async... DefaultAccountSerializationContainer accountContainer = (DefaultAccountSerializationContainer)deserializedObject; this.Accounts = new ObservableCollection <Account>(accountContainer.GetAccountCollection(this)); } } catch (InvalidOperationException) { initialLoadFailed = true; XmlSerializer x = new XmlSerializer(typeof(LegacyAccountManager)); using (StreamReader reader = new StreamReader(accountFileName)) { object deserializedObject = x.Deserialize(reader); //TODO: This is not technically async... LegacyAccountManager legacyAccountManager = (LegacyAccountManager)deserializedObject; this.Accounts = new ObservableCollection <Account>(legacyAccountManager.CreateAccounts(this)); } } //One time write back to change format of the file if (initialLoadFailed) { await this.SaveAccountsAsync().ConfigureAwait(false); } }
/// <summary> /// Save the accounts to the account file /// </summary> private async Task SaveAccountsAsync() { string fileName = GetAccountFileName(); IEnumerable<DefaultAccount> defaultAccountList = this.Accounts.Cast<DefaultAccount>(); DefaultAccountSerializationContainer serializationContainer = new DefaultAccountSerializationContainer(defaultAccountList); XmlSerializer x = new XmlSerializer(typeof(DefaultAccountSerializationContainer)); using (var writer = new StreamWriter(fileName, false)) { x.Serialize(writer, serializationContainer); await writer.FlushAsync().ConfigureAwait(false); } }