Skip to content

SharpConfig is an easy-to-use CFG/INI configuration library for .NET.

License

Notifications You must be signed in to change notification settings

ntodorov/SharpConfig

Repository files navigation

sharpconfig_logo.png

SharpConfig is an easy-to-use CFG/INI configuration library for .NET.

You can use SharpConfig in your .NET applications to add the functionality to read, modify and save configuration files and streams, in either text or binary format.

If SharpConfig has helped you and you feel like donating, feel free! Donations help to keep the development of SharpConfig active.

A configuration file example:

[General] # a comment SomeString = Hello World! SomeInteger = 10# an inline comment SomeFloat = 20.05 ABoolean = true

To read these values, your C# code would look like:

Configurationconfig=Configuration.LoadFromFile("sample.cfg");Sectionsection=config["General"];stringsomeString=section["SomeString"].Value;intsomeInteger=section["SomeInteger"].GetValue<int>();floatsomeFloat=section["SomeFloat"].GetValue<float>();

Enumerations

SharpConfig is also able to parse enumerations. For example you have a configuration like this:

[DateInfo] Day = Monday

It is now possible to read this value as a System.DayOfWeek enum, because Monday is present there. An example of how to read it:

DayOfWeekday=config["DateInfo"]["Day"].GetValue<DayOfWeek>();

Arrays

Arrays are also supported in SharpConfig. For example you have a configuration like this:

[General] MyArray ={0,2,5,6}

This array can be interpreted as any type array that can be converted from 0, 2, 5 and 6, for example int, float, double, char, byte, string etc.

Reading this array is simple:

object[]myArray=config["General"]["MyArray"].GetValue<object[]>();

Creating a Configuration in-memory

// Create the configuration.varmyConfig=newConfiguration();// Set some values.// This will automatically create the sections and settings.myConfig["Video"]["Width"].Value="1920";myConfig["Video"]["Height"].Value="1080";// Set an array value.myConfig["Video"]["Formats"].SetValue(newstring[]{"RGB32","RGBA32"});

Iterating through a Configuration

foreach(varsectioninmyConfig){foreach(varsettinginsection){ ...}}

Saving a Configuration

myConfig.Save("myConfig.cfg");// Save to a text-based file.myConfig.Save(myStream);// Save to a text-based stream.myConfig.SaveBinary("myConfig.cfg");// Save to a binary file.myConfig.SaveBinary(myStream);// Save to a binary stream.

Object Mapping

A nice-to-have in SharpConfig is the mapping of sections to objects. If you have a class and enumeration in C# like this:

classPerson{publicstringName{get;set;}publicintAge{get;set;}publicGenderGender{get;set;}}enumGender{Male,Female}

It is possible to create an object straight from the configuration file:

[Person] Name = Peter Age = 50 Gender = Male

Like this:

Personperson=config["Person"].CreateObject<Person>();

Note: The mapping only works on classes, public properties and primitive data types (int, bool, enums ...).

If you already have a Person object and don't want to create a new one, you can use the MapTo method:

config["Person"].MapTo(person);

Installing via NuGet

You can install SharpConfig via the following NuGet command:

Install-Package sharpconfig

NuGet Page

About

SharpConfig is an easy-to-use CFG/INI configuration library for .NET.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages