Configs 1.0.3
See the version list below for details.
dotnet add package Configs --version 1.0.3
NuGet\Install-Package Configs -Version 1.0.3
<PackageReference Include="Configs" Version="1.0.3" />
paket add Configs --version 1.0.3
#r "nuget: Configs, 1.0.3"
// Install Configs as a Cake Addin
#addin nuget:?package=Configs&version=1.0.3
// Install Configs as a Cake Tool
#tool nuget:?package=Configs&version=1.0.3
Configs
https://www.nuget.org/packages/Configs/
A simple way to save the configurations of a C# app as a json file.
The json file(s) is saved in AppData/Local and there is currently no way to change that.
How to use
Before we start, install the package from NuGet.
This package uses Newtonsoft.Json and the idea is to make a class that's going to be used as the model for the configuration file.
Setup
Make a class that inherits from ConfigsTools. This class is the model for your configurations file, so it details how the json file is going to look like.
using Configs;
using Newtonsoft.Json;
namespace MyApp
{
class AppConfigs : ConfigsTools
{
public AppConfigs()
{
ConfigsFileName = "MyConfigurations.json";
}
[JsonProperty("activated", Required = Required.AllowNull)]
public bool Activated { get; set; }
[JsonProperty("randomString", Required = Required.Default)]
public string RandomString { get; set; }
[JsonProperty("importantStuff", Required = Required.AllowNull)]
public List<ImportantThing> ImportantStuff { get; set; }
public ImportantThing GetImportantThing(string owner)
{
return ImportantStuff.Find(x => x.OwnerName.Equals(owner));
}
}
}
Pretty much anything supported by Newtonsoft.Json and is serializable can be used in this model. You can of course make multiple models each with a different ConfigsFileName to have multiple configurations files.
Usage:
Getting the file's values:
// Get the current app configurations from the json configurations file.
AppConfigs appConfigs = Configs.ConfigsTools.GetConfigs<AppConfigs>();
// Get a value from the configurations file.
bool appConfigActivated = appConfigs.Activated;
// This the value at time GetConfigs was called. If you want updated values then you have to call GetConfigs again.
Editing and saving the file:
// Get the current app configurations from the json configurations file.
AppConfigs appConfigs = Configs.ConfigsTools.GetConfigs<AppConfigs>();
// Change value.
appConfigs.Activated = false;
// Save the file.
appConfigs.Save();
Result
{
"activated": true,
"randomString": "Hey",
"importantStuff": [
{
"ownerName": "Elias Youssef"
},
{
"ownerName": "Slim Shady"
}
]
}
<a target="_blank" href="https://icons8.com/icons/set/window-settings">Window Settings icon</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>
Product | Versions |
---|---|
.NET | net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows |
.NET Core | netcoreapp3.0 netcoreapp3.1 |
.NET Standard | netstandard2.1 |
MonoAndroid | monoandroid |
MonoMac | monomac |
MonoTouch | monotouch |
Tizen | tizen60 |
Xamarin.iOS | xamarinios |
Xamarin.Mac | xamarinmac |
Xamarin.TVOS | xamarintvos |
Xamarin.WatchOS | xamarinwatchos |
-
.NETStandard 2.1
- LocalAppDataFolder (>= 1.0.3)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.