Skip to content

Latest commit

 

History

History
executable file
·
84 lines (59 loc) · 2.97 KB

File metadata and controls

executable file
·
84 lines (59 loc) · 2.97 KB

SharpConfig

Easy to use cfg / ini configuration library for .NET.

You can use SharpConfig to read, modify and save configuration files and streams, in either text or binary format.

Minimalistic, fully portable (.NET Standard 2.0), zero package dependencies.

Website NuGet Version NuGet Downloads .NET

Install

Example

# An example configuration:

[General]
# a comment
SomeString = Hello World!
SomeInteger = 10 # an inline comment
SomeFloat = 20.05
SomeBoolean = true
SomeArray = { 1, 2, 3 }
Day = Monday

[Person]
Name = Peter
Age = 50

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

var config = Configuration.LoadFromFile("sample.cfg");
var section = config["General"];

string    someString      = section["SomeString"].StringValue;
int       someInteger     = section["SomeInteger"].IntValue;
float     someFloat       = section["SomeFloat"].FloatValue;
bool      someBool        = section["SomeBoolean"].BoolValue;
int[]     someIntArray    = section["SomeArray"].IntValueArray;
string[]  someStringArray = section["SomeArray"].StringValueArray;
DayOfWeek day             = section["Day"].GetValue<DayOfWeek>();

// Entire user-defined objects can be created from sections and vice versa.
var person = config["Person"].ToObject<Person>();
// ...

Behavior Notes

  • config["SectionName"] and section["SettingName"] are create-or-get APIs. If the requested item does not exist, SharpConfig creates it and returns it.
  • GetSectionsNamed() and GetSettingsNamed() use StringComparison.OrdinalIgnoreCase by default to preserve existing behavior.
var config = new Configuration();

config["General"]["Value"].IntValue = 50;

// Default behavior is case-insensitive.
var matchingSections = config.GetSectionsNamed("general");

// Use StringComparison.Ordinal for case-sensitive matching.
var strictSections = config.GetSectionsNamed("General", StringComparison.Ordinal);

Documentation

The full documentation is available on the website.