/// <summary> /// Asynchronously write the local storage to disk. /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task Write() { try { using var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); using var stream = isolatedStorage.OpenFile(ProtectedStorageFileName, FileMode.Create, FileAccess.Write, FileShare.None); using var dataProtectionStream = new DataProtectionStream(stream, System.Security.Cryptography.DataProtectionScope.CurrentUser); await JsonSerializer.SerializeAsync(dataProtectionStream, _values).ConfigureAwait(false); dataProtectionStream.FlushFinalBlock(); dataProtectionStream.Close(); stream.Close(); } finally { _writeTask = null; } }
/// <summary> /// Asynchronously read the local storage from disk. /// </summary> /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> private async Task Read() { try { using var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); if (isolatedStorage.FileExists(ProtectedStorageFileName)) { using var stream = isolatedStorage.OpenFile(ProtectedStorageFileName, FileMode.Open, FileAccess.Read, FileShare.None); using var dataProtectionStream = new DataProtectionStream(stream, System.Security.Cryptography.DataProtectionScope.CurrentUser); _values = await JsonSerializer.DeserializeAsync<ConcurrentDictionary<string, string>>(dataProtectionStream).ConfigureAwait(false); dataProtectionStream.Close(); stream.Close(); } if (_values == null) { _values = new ConcurrentDictionary<string, string>(); } } finally { _readTask = null; } }