All posts

QuickConfig

Time for a post about something I made: QuickConfig, the only NuGet package I’ve published so far.

QuickConfig is a small library for handling configuration in .NET apps. You define a class with your settings, and QuickConfig takes care of loading and saving it. Very little code, no fuss.

I originally wrote it for my own hobby projects. By now I use it all the time.

So how does it work?

Step 1: Define a configuration class

Create a class that inherits from ConfigBase and add your settings as properties.

using QuickConfig;

public class Config : ConfigBase
{
    public string SettingOne { get; set; }
    public string SettingTwo { get; set; }
}

Step 2: Create a ConfigManager and load your config

Create a ConfigManager and give it a name. That name becomes the folder your config is stored in. In the full example at the bottom I use the assembly name for the folder and “Config” for the file. Then load the config:

// 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<Config>("ConfigName");

Step 3: Change a setting and save

Now you can change your 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’s everything together:

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");

        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();
    }
}