Last active
December 1, 2022 06:20
-
-
Save olejorgensen/17df1a7caeb88fa1ddfce43be30dde52 to your computer and use it in GitHub Desktop.
portable application settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : YourApp | |
{ | |
private static SettingsManager Manager = new SettingsManager("./settings.json"); | |
public static Settings Settings { get { return Manager.Settings; } } | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp.Configuration; | |
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using CommunityToolkit.Mvvm.ComponentModel; | |
public partial class Settings : ObservableObject | |
{ | |
public Settings() : base() | |
{ | |
} | |
[ObservableProperty] | |
private Uri source = new Uri("https://gist.github.com/"); | |
[ObservableProperty] | |
private bool shouldDoThis = false; | |
[ObservableProperty] | |
private bool shouldDoThat = true; | |
[ObservableProperty] | |
private int timeToWait = 3600; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MyApp.Configuration; | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Text.Json; | |
using System.Threading.Tasks; | |
public class SettingsManager | |
{ | |
private Settings settings = new Settings(); | |
public Settings Settings { get => settings; set => settings = value; } | |
public string FileName; | |
public SettingsManager(string fileName) | |
{ | |
FileName = fileName ?? throw new ArgumentNullException(nameof(fileName)); | |
Load(); | |
settings.PropertyChanged += OnSettingsChanged; | |
} | |
private void OnSettingsChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) | |
{ | |
Task.Run(SaveAsync).Wait(); | |
} | |
private void CreateDirectory() | |
{ | |
var dir = Path.GetDirectoryName(FileName); | |
if (!Directory.Exists(dir)) | |
{ | |
Directory.CreateDirectory(dir); | |
} | |
} | |
private void Load() | |
{ | |
CreateDirectory(); | |
if (!File.Exists(FileName)) | |
{ | |
SaveAsync(); | |
return; | |
} | |
var jsonString = File.ReadAllText(FileName, Encoding.UTF8); | |
var settings = JsonSerializer.Deserialize<Settings>(jsonString); | |
this.Settings = settings; | |
} | |
private async Task SaveAsync() | |
{ | |
CreateDirectory(); | |
using FileStream createStream = File.Create(FileName); | |
await JsonSerializer.SerializeAsync(createStream, Settings); | |
await createStream.DisposeAsync(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment