I thought it would be a great idea to dedicate a blog post to one of my creations: QuickConfig, the only NuGet package I’ve published so far.
QuickConfig is a lightweight configuration management library designed for .NET applications. It simplifies the process of defining configuration classes and managing their settings, making it easy to load, save, and maintain configurations with minimal effort.
Initially developed for my hobby projects, it has since become an indispensable tool that I regularly rely on.
So, how is it to be used?
Step 1: Define A Configuration Class
First, create a class that inherits from ConfigBase
and define your configuration properties.
using QuickConfig;
public class Config : ConfigBase
{
public string SettingOne { get; set; }
public string SettingTwo { get; set; }
}
Step 2: Initialize ConfigManager and Load Configuration
Create an instance of ConfigManager
with a name (in the example below I use the assembly name as the folder name, and Config as the file name) and load the configuration.
// Replace FolderName with the name of the folder to store the configration in.
// Use System.Reflection.Assembly.GetExecutingAssembly().GetName().Name for the project name.
ConfigManager configManager = new ConfigManager("FolderName");
// Replace ConfigName with the name of the config file within the project.
// A project can have multiple files. Just use Config if you only want one.
Config config = configManager.GetConfig("ConfigName");
Step 3: Modify and Save Configuration
You can now modify your configuration settings and save them.
config.SettingOne = "Test";
if (config.SettingOne == "Test")
{
Console.WriteLine("Config setting was set correctly.");
}
else
{
Console.WriteLine("Config setting was not set correctly.");
}
config.Save();
Complete Example
Here is the complete example code:
using QuickConfig;
public class Config : ConfigBase
{
public string SettingOne { get; set; }
public string SettingTwo { get; set; }
}
public class Program
{
public static void Main()
{
ConfigManager configManager = new ConfigManager(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
Config config = configManager.GetConfig("Config");
config.SettingOne = "Test";
if (config.SettingOne == "Test")
{
Console.WriteLine("Config setting was set correctly.");
}
else
{
Console.WriteLine("Config setting was not set correctly.");
}
config.Save();
}
}