SharpConfig
SharpConfig is an 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.
Install via:
- .NET CLI:
> dotnet add package sharpconfig
- Package Manager:
> NuGet\Install-Package sharpconfig
- Download latest
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>();
// ...
More examples
Options
Sometimes a project has special configuration files or other needs, for example ignoring all comments in a file.
To allow for greater flexibility, SharpConfig's behavior is
modifiable using static properties of the Configuration
class.
The following properties are available:
Option | Type | Description |
---|---|---|
CultureInfo |
CultureInfo |
Gets or sets the CultureInfo that is used for value conversion in SharpConfig. The default value is CultureInfo.InvariantCulture . |
ValidCommentChars |
char[] |
Gets the array that contains all valid comment delimiting characters. The default value is { '#', ';' } |
PreferredCommentChar |
char |
Gets or sets the preferred comment char when saving configurations. The default value is '#' . |
ArrayElementSeparator |
char |
Gets or sets the array element separator character for settings. The default value is ',' . Remember that after you change this value while Setting instances exist, to expect their ArraySize and other array-related values to return different values. |
IgnoreInlineComments |
bool |
Gets or sets a value indicating whether inline comments should be ignored when parsing a configuration. |
IgnorePreComments |
bool |
Gets or sets a value indicating whether pre-comments should be ignored when parsing a configuration. |
SpaceBetweenEquals |
bool |
Gets or sets a value indicating whether space between equals should be added when saving a configuration. |
OutputRawStringLiterals |
bool |
Gets or sets a value indicating whether string values are written without quotes, but including everything in between. For example, a setting MySetting=" Example value" would be written to a file as MySetting= Example value . |
Ignoring properties, fields and types
Suppose you have the following class:
SharpConfig will now ignore the SomeInt
property when creating sections from objects of type
SomeClass
and vice versa. Now suppose you have a type in your project that should always be ignored.
You would have to mark every property that returns this type with a [SharpConfig.Ignore]
attribute. An easier solution is to just apply the [SharpConfig.Ignore]
attribute to the type.
instead of:
Redundant attributes | |
---|---|
Note
This ignoring mechanism works the same way for public fields.
Adding custom object converters
There may be cases where you want to implement conversion rules for a custom type, with
specific requirements. This is easy and involves two steps, which are illustrated
using the Person
example:
Step 1: Create a custom converter class that derives from SharpConfig.TypeStringConverter<T>
.
Step 2: Register the PersonStringConverter
(anywhere you like):
That's it!
Whenever a Person
object is used on a Setting
(via GetValue()
and SetValue()
),
your converter is selected to take care of the conversion.
This also automatically works with SetValue()
for arrays and GetValueArray()
.