-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/Eliasyoussef47/Configs
- Loading branch information
Showing
1 changed file
with
86 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,103 @@ | ||
![Configs icon](https://raw.githubusercontent.com/Eliasyoussef47/Configs/master/Configs/Assets/icons8-window-settings-96.png) | ||
|
||
# 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. | ||
The json file(s) is saved in AppData/Local/{YourAppName} 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](https://github.com/JamesNK/Newtonsoft.Json) and the idea is to make a class that's going to be used as the model for the configuration file. | ||
This package uses [Newtonsoft.Json](https://github.com/JamesNK/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. | ||
Model example: | ||
|
||
using Configs; | ||
using Newtonsoft.Json; | ||
```C# | ||
using Configs; | ||
using Newtonsoft.Json; | ||
|
||
namespace MyApp | ||
namespace MyApp | ||
{ | ||
class AppConfigs : ConfigsTools | ||
{ | ||
public AppConfigs() | ||
{ | ||
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("registeredTriggerSenders", Required = Required.AllowNull)] | ||
public List<ImportantThing> ImportantStuff { get; set; } | ||
|
||
public ImportantThing GetImportantThing(string owner) | ||
{ | ||
return ImportantStuff.Find(x => x.OwnerName.Equals(owner)); | ||
} | ||
} | ||
ConfigsFileName = "MyConfigurations.json"; | ||
} | ||
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: | ||
[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 | ||
|
||
```C# | ||
using Configs; | ||
|
||
... | ||
|
||
// Get the current app configurations from the json configurations file. | ||
AppConfigs appConfigs = ConfigsTools.GetConfigs<AppConfigs>(); | ||
// Get a value from the configurations file. | ||
bool appConfigActivated = appConfigs.Activated; | ||
``` | ||
|
||
### Editing and saving the file | ||
|
||
```C# | ||
using Configs; | ||
|
||
... | ||
|
||
// Get the current app configurations from the json configurations file. | ||
AppConfigs appConfigs = ConfigsTools.GetConfigs<AppConfigs>(); | ||
// Change value. | ||
appConfigs.Activated = false; | ||
// Save the file. | ||
appConfigs.Save(); | ||
``` | ||
|
||
// 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. | ||
## Result | ||
|
||
### Editing and saving the file: | ||
```json | ||
{ | ||
"activated": false, | ||
"randomString": "Hey", | ||
"importantStuff": [ | ||
{ | ||
"ownerName": "Elias Youssef" | ||
}, | ||
{ | ||
"ownerName": "Slim Shady" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
// 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(); | ||
[Window Settings icon](https://icons8.com/icons/set/window-settings) icon by [Icons8](https://icons8.com) |