A simple library to add a key-value pair based configuration manager to any .NET project.
- Add
Config.dllorConfig.csto your project - Load (or create a new) key and value set with
Config.Set(filePath) - Get values from existing keys with
Config.GetValue() - Set values for new or existing keys with
Config.SetValue() - Save your keys and values for the next session with
Config.Save()
Values are stored as a
string. An appropriate parsing procedure must be used to convert it to and from other types.
For a code example, see ConfigExample/Program.cs
The above code example will create test_config.json in the user documents folder with the following content:
{
"name": "John Doe",
"ip_address": "127.0.0.1",
"port": "1234"
}Create and use multiple value sets by subsequent calls to Set
string set1 = "first.json";
string set2 = "second.json";
// Create a value set
Config.Set(set1);
Config.SetValue("name", "Jane Doe");
Config.Save();
// Create another value set
Config.Set(set2);
Config.SetValue("height", "1080");
Config.SetValue("width", "1920");
Config.Save();
// Load and use the first value set
Config.Set(set1);
Console.WriteLine($"name:\t{Config.GetValue("name", "Failsafe Frank")}");
// Load and use the second value set
Config.Set(set2);
Console.WriteLine($"height:\t{Config.GetValue("height", "999")}");
Console.WriteLine($"width:\t{Config.GetValue("width", "999")}");| Method | Description | Parameters | Returns |
|---|---|---|---|
string FilePath |
Get the current value set file path | The current value set file path | |
void Set(string filePath) |
Set (and load) the value set file to use | filePath: Value set file to use |
|
bool KeyExists(string key) |
Check if a key exists | key: Unique key |
True, if the key exists |
string GetValue(string key) |
Get the current value for a key | key: Unique key |
Current value for the key or null |
string GetValue(string key, string defaultValue) |
Get the current value or a default value for a key | key: Unique keydefaultValue: Value to return if the key does not exist |
Current value for the key or defaultValue |
Dictionary<string, string> GetAllValues() |
Get the entire value set | Current value set | |
void SetValue(string key, string value) |
Set a new value for a key or create a new key with a value | key: Unique keyvalue: New value |
|
void SetAllValues(Dictionary<string, string> values) |
Overwrite the entire value set | values: New value set |
|
void Save() |
Save the current value set |
All the methods are static, no
Configinstance needs to be created.
- Keys and values are stored in a
Dictionary, so no duplicate keys can exist. - The file path can include or exclude the
.jsonpostfix. It will be appended to it if missed. Savemust be called before subsequentSetcalls if any values were added or changed by any of theSet*methods. If missed, an exception will be thrown with the messageThere are unsaved changes that would be lost. Call Save() first.- If the config file does not exist when
Setis called, the config manager will start creating the keys and values in memory. The next call toSavewill create the file. - If any exception is thrown during file loading inside the
Setmethod, an exception will be thrown with the messageError loading config file.and the original exception as theinnerException. - The value set must be set with
Setbefore callingKeyExists,Saveor any of theGetorSet*methods. If any of these are called without the value set being set, an exception will be thrown with the messageNo current value set is set. Call Set() before making changes. - The
GetValuemethod with thedefaultValueparameter is to provide a failsafe value retrieval. - A call to
GetAllValueswill return a copy of the underlyingDictionaryto protect it's values in the absence of a call toSetAllValues. - If any exception is thrown during the
Savemethod, an exception will be thrown with the messageError saving config file.and the original exception as theinnerException.
flowchart LR
B[Set] --> C[KeyExists]
B --> D[GetValue]
B --> E[GetAllValues]
B --> F[SetValue]
B --> G[SetAllValues]
B --Will create an empty file--> H[Save]
C --> H
D --> H
E --> H
F --> H
G --> H
H --Except the first time--> B