From 9bc464ad946a80bbd6bac55ebe24659f7430f337 Mon Sep 17 00:00:00 2001 From: Mahdi Hosseini Date: Thu, 25 Jun 2026 19:56:03 +0330 Subject: [PATCH] Remove JsonSettingsProvider and Use ApplicationDataSettingsProvider for UnPackaged #2191 --- .../Providers/JsonSettingsProvider.cs | 103 ------------------ .../Providers/SettingsProviderFactory.cs | 11 +- WinUIGallery/WinUIGallery.csproj | 1 + 3 files changed, 2 insertions(+), 113 deletions(-) delete mode 100644 WinUIGallery/Helpers/SettingsHelper/Providers/JsonSettingsProvider.cs diff --git a/WinUIGallery/Helpers/SettingsHelper/Providers/JsonSettingsProvider.cs b/WinUIGallery/Helpers/SettingsHelper/Providers/JsonSettingsProvider.cs deleted file mode 100644 index 15dd4a63f..000000000 --- a/WinUIGallery/Helpers/SettingsHelper/Providers/JsonSettingsProvider.cs +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using System.Collections.Generic; -using System.IO; -using System.Text.Json; - -namespace WinUIGallery.Helpers; - -public partial class JsonSettingsProvider : ISettingsProvider -{ - private readonly string filePath; - private Dictionary values = new(); - - public JsonSettingsProvider(string filePath) - { - this.filePath = filePath; - Load(); - } - - public bool Contains(string key) => values.ContainsKey(key); - - public object? Get(string key) => values.TryGetValue(key, out var value) ? value : null; - - public void Set(string key, object value) - { - var json = JsonSerializer.SerializeToElement(value, SettingsJsonContext.Default.GetTypeInfo(value.GetType())!); - values[key] = json; - Save(); - } - - public T? Get(string key) - { - if (!values.TryGetValue(key, out var jsonElement)) - return default; - - try - { - return (T?)jsonElement.Deserialize(SettingsJsonContext.Default.GetTypeInfo(typeof(T))!); - } - catch (Exception) - { - HandleCorruptedKey(key); - return default; - } - } - - public void Set(string key, T value) - { - var json = JsonSerializer.SerializeToElement(value, SettingsJsonContext.Default.GetTypeInfo(typeof(T))!); - values[key] = json; - Save(); - } - - private void Load() - { - if (!File.Exists(filePath)) - return; - - try - { - var json = File.ReadAllText(filePath); - values = JsonSerializer.Deserialize( - json, - SettingsJsonContext.Default.DictionaryStringJsonElement - ) ?? new(); - } - catch (Exception) - { - HandleCorruptedFile(); - } - } - private void HandleCorruptedFile() - { - try - { - File.Delete(filePath); - } - catch (Exception ex) - { - System.Diagnostics.Debug.WriteLine($"Failed to delete corrupted settings file '{filePath}': {ex}"); - } - - values = new(); - } - - private void HandleCorruptedKey(string key) - { - values.Remove(key); - Save(); - } - - - private void Save() - { - var json = JsonSerializer.Serialize( - values, - SettingsJsonContext.Default.DictionaryStringJsonElement - ); - File.WriteAllText(filePath, json); - } -} diff --git a/WinUIGallery/Helpers/SettingsHelper/Providers/SettingsProviderFactory.cs b/WinUIGallery/Helpers/SettingsHelper/Providers/SettingsProviderFactory.cs index 5d699da4e..301722faa 100644 --- a/WinUIGallery/Helpers/SettingsHelper/Providers/SettingsProviderFactory.cs +++ b/WinUIGallery/Helpers/SettingsHelper/Providers/SettingsProviderFactory.cs @@ -1,6 +1,4 @@ using Microsoft.Windows.Storage; -using System; -using System.IO; namespace WinUIGallery.Helpers; @@ -14,14 +12,7 @@ public static ISettingsProvider CreateProvider() } else { - var folder = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), - ProcessInfoHelper.ProductName - ); - - Directory.CreateDirectory(folder); - var filePath = Path.Combine(folder, "AppConfig.json"); - return new JsonSettingsProvider(filePath); + return new ApplicationDataSettingsProvider(ApplicationData.GetForUnpackaged(ProcessInfoHelper.Publisher, ProcessInfoHelper.ProductName).LocalSettings); } } } diff --git a/WinUIGallery/WinUIGallery.csproj b/WinUIGallery/WinUIGallery.csproj index 65b4ce66a..548d80272 100644 --- a/WinUIGallery/WinUIGallery.csproj +++ b/WinUIGallery/WinUIGallery.csproj @@ -43,6 +43,7 @@ Assets\Tiles\GalleryIcon.ico enable true + Microsoft