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. The library is backward compatible up to .NET 2.0.
If SharpConfig has helped you and you feel like donating, feel free! Donations help to keep the development of SharpConfig active.
You can install SharpConfig via the following NuGet command:
Install-Package sharpconfig
[General]
# a comment
SomeString = Hello World!
SomeInteger = 10 # an inline comment
SomeFloat = 20.05
SomeBoolean = true
To read these values, your C# code would look like:
Configuration config = Configuration.LoadFromFile("sample.cfg");
Section section = config["General"];
string someString = section["SomeString"].StringValue;
int someInteger = section["SomeInteger"].IntValue;
float someFloat = section["SomeFloat"].FloatValue;
bool someBool = section["SomeBoolean"].BoolValue;
foreach (var section in myConfig)
{
foreach (var setting in section)
{
// ...
}
}
// Create the configuration.
var myConfig = new Configuration();
// Set some values.
// This will automatically create the sections and settings.
myConfig["Video"]["Width"].IntValue = 1920;
myConfig["Video"]["Height"].IntValue = 1080;
// Set an array value.
myConfig["Video"]["Formats"].SetValue( new string[] { "RGB32", "RGBA32" } );
// Get the values just to test.
int width = myConfig["Video"]["Width"].IntValue;
int height = myConfig["Video"]["Height"].IntValue;
string[] formats = myConfig["Video"]["Formats"].GetValueArray<string>();
// ...
myConfig.SaveToFile("myConfig.cfg"); // Save to a text-based file.
myConfig.SaveToStream(myStream); // Save to a text-based stream.
myConfig.SaveToBinaryFile("myConfig.cfg"); // Save to a binary file.
myConfig.SaveToBinaryStream(myStream); // Save to a binary stream.
SharpConfig has more features, such as support for arrays, enums and object mapping. For details and examples, please visit the Wiki. There are also use case examples available in the Example File.