A very simple library for creating easy to use to use application settings that use json serialization.
This library allows you to quickly and easily create application settings classes. I generally use this in my WPF apps.
- .Net Standard 2+,.Net 6+
nuget install ApplicationOptions
public class AppSettings : BaseOptions<AppSettings>
{
// Override base folder
protected override string BaseFolder => @"settings\";
// Override file name (Default is $"options.{typeof(T).Name}.json")
protected override string FileName => "appsettings.json";
public DateTime LastLaunch { get; set; }
public string ApiKey { get; set; }
public string ApiEndpoint { get; set; }
//Sets a default value when initializing new settings
public Rectangle WindowPosition { get; set; } = new Rectangle(100, 100, 850, 900);
}
BaseOptionSettings.WatchForOptionChanges = true;
AppSettings.Instance.Loaded += () => Console.WriteLine("AppSettings Loaded");
AppSettings.Instance.Saved += () => Console.WriteLine("AppSettings Saved");
public class MyWindow : Window
{
private Settings => AppSettings.Instance;
public MyWindow()
{
Top = Settings.WindowPosition.Y;
Left = Settings.WindowPosition.X;
Width = Settings.WindowPosition.Width;
Height = Settings.WindowPosition.Height;
}
private void OnClosing()
{
BaseOptionSettings.LoadedOptions.ForEach(o => o.Save());
}
}
The BaseOptionSettings
class lets you configure how the BaseOptions class.
// Watch for changes in the settings files
BaseOptionSettings.WatchForOptionChanges = true;
// Save all loaded options
BaseOptionSettings.LoadedOptions.ForEach(o => o.Save());
// Indent the json files or not
BaseOptionSettings.IndentJson = false;
- Fork and clone locally
- Create a topic specific branch. Add some nice feature.
- Send a Pull Request to spread the fun!