If you want to add a settings to your app and you like json, then this is what you need. This uses the standard interface for working with settings, which is provided by Microsoft.Extensions.Configuration. To do this, we added methods for changing values in settings via the standard interface, which will edit the json file themselves.
Add Nuget Package in your project:
Install-Package WritableJsonConfiguration
IConfigurationRoot configuration = WritableJsonConfigurationFabric.Create("Settings.json");Use this configuration in the app as you need, usually people register it with the IoC, example(Splat):
Locator.CurrentMutable.RegisterConstant(configuration, typeof(IConfiguration));Themes theme = configuration.GetSection("Appearance:Theme").Get<Themes>();or
Themes theme = configuration.Get<Themes>("Appearance:Theme");configuration.GetSection("Appearance:Theme").Set(theme);or
configuration.Set("Appearance:Theme", theme);IConfigurationRoot configuration = WritableJsonConfigurationFabric.Create(source =>
{
source.Path = "Settings.json";
source.Optional = true;
source.ReloadOnChange = false;
source.UseAtomicWrites = true;
source.ResolveFileProvider();
});UseAtomicWrites defaults to false, preserving the previous API and platform behavior.
When enabled, both Set overloads serialize writes to the same physical path within
the process. A complete, parser-validated temporary file is flushed and replaces the
main file; .bak contains the previous readable version. A first save creates only
the main file. No-op saves do not rotate the backup. Memory is published after the
file commit; ambiguous I/O errors reread disk and block further writes if reconciliation
fails. Restart the application or recreate the configuration root before trying
again in that case; calling Reload() on the same root does not unblock writes.
Numeric path segments remain property names inside JSON objects. Inside arrays they
are indexes; sparse indexes are rejected rather than redirected to another element.
Temporary files receive the source file's restricted Windows access permissions
before any settings bytes are written. Existing main/backup permission differences
that cannot safely be preserved cause an error, not a broader copy. The first file
uses the containing directory's permissions. Other operating systems reject explicit
opt-in with PlatformNotSupportedException; the default mode remains available.
This does not recover an already corrupt file, coordinate multiple processes, make a
series of Set calls transactional, or guarantee survival of arbitrary hardware
failure. Applications should validate/recover settings before loading configuration.
Backups and leftover temporary files may contain secrets and must not be published.
Any suggestions and comments are welcome. If you want to contact me, use Telegram
