Set it in!
A very simple and straightforward way of working with user and application settings in .NET
Access, create, and persist user and application settings at run time using the following implementations:
- Memory Settings
- JSON Settings
- XML Settngs
Targets .NET Standard 1.3
Installation
For XML:
Install-Package SetItIn.Xml
For JSON:
Install-Package SetItIn.Json
Usage example
var localAppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var jsonFile = Path.Combine(appDataFolder, "MyApp", "MySettings.json");
ISettings settings = new JsonSettings(jsonFile);
settings.Set("Chuck", "Norris");
settings.Set("Walker, Texas Ranger episodes", 201);
settings.Set("Can Chuck Norris divide by zero?", true);
if (settings.Get("Walker, Texas Ranger episodes", out int episodes))
Console.WriteLine($"8 seasons and {episodes} episodes.");
Don't forget to save the settings
settings.Save();
Load them from the file at any time
settings.Load();
It does work with reference types as well
public class UserSetting
{
public string StringValue { get; set; }
public double DoubleValue { get; set; }
public bool BooleanValue { get; set; }
public DateTime? DateTimeValue { get; set; }
}
settings.Set("MySetting", new UserSetting
{
StringValue = "A",
DoubleValue = Math.PI,
BooleanValue = true,
DateTimeValue = DateTime.MinValue
});
// ... save it on shutdown
settings.Save();
// ... load it on startup
settings.Load();
if (settings.Get("MySetting", out UserSetting mySetting))
// do something with it
JSON settings file would look like
{
"Ref": {
"StringValue": "A",
"DoubleValue": 3.141592653589793,
"BooleanValue": true,
"DateTimeValue": "0001-01-01T00:00:00"
}
}
And XML
<settings>
<s k="Ref">
<UserSetting>
<StringValue>A</StringValue>
<DoubleValue>3.141592653589793</DoubleValue>
<BooleanValue>true</BooleanValue>
<DateTimeValue>0001-01-01T00:00:00</DateTimeValue>
</UserSetting>
</s>
</settings>
Contributing
- Fork it (https://github.com/dkjazz/SetItIn)
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request