Skip to content

A slim, fully managed C# library for reading/writing .ini, .conf, .cfg etc configuration files.

License

Notifications You must be signed in to change notification settings

salaros/config-parser

Repository files navigation

ConfigParser Build status AppVeyor tests branch Coverage Status

GitHub top language .NET Standard .NET Standard .NET Framework

License NuGet NuGet Pre Release NuGet

Donate Patreon Donate Paypal Donate Liberapay

ConfigParser - is a slim, cross-platform, fully managed C# library for reading and writing .ini, .conf, .cfg etc configuration files.

You could use it in your Unity 3D, Xamarin (iOS & Android), .NET Framework applications (even with old 4.0/4.5), .NET Core CLI and ASP.NET Core applications, Mono etc

Features

Customization

  • customizable encoding (most encodings can be auto-detected)
  • customizable culture
  • customizable number styles (e.g. currencies, exponential notation etc)
  • customizable line endings (usually auto-detected)
  • customizable true and false (e.g. "verum" / "falsum" )
  • customizable comment characters
  • customizable key/value separator (defaults to '=')

Read and preserved

  • file header comments
  • config files with no sections (like in this [SectionName]) or even mixed: first section has no header, just keys
  • comments in general
  • section comments
  • empty lines
  • indented sections
  • indented keys
  • indented values

Values

  • default values
  • multi-line values (both quoted and not)
  • quoted values
  • null values (value-less keys)
  • array values
  • fancy float / double
  • byte-encoded values
  • smart boolean values (0/1, on/off, enabled/disabled work of the box)

and more...

🟊🟊🟊 Support this project 🟊🟊🟊

You can support us in a small way, please consider starring and sharing this repo! It helps us getting known and grow the community.

star us

Installation

Config Parser can be installed via NuGet by using Package Manager in your IDE, dotnet binary or Package Console

# Add the Salaros.ConfigParser package to a project named [<PROJECT>]
dotnet add [<PROJECT>] package Salaros.ConfigParser

or Visual Studio's Package Console

# Add the Salaros.ConfigParser package to the default project
Install-Package Salaros.ConfigParser

# Add the Salaros.ConfigParser package to a project named [<PROJECT>]
Install-Package Salaros.ConfigParser -ProjectName [<PROJECT>]

Usage

// Initialize config file instance from file
var configFileFromPath = new ConfigParser(@"path\to\configfile.cnf");

// Parse text
var configFileFromString = new ConfigParser(@"
    [Strings]
        canBeIndented = value
    andQuoted = ""quotes will be stripped""

    [Numbers]
    withD = 0.6D
    dollars = $2,999

    [boolean]
    numericTrue = 1
    textFalse = true
    yesWorks = yes
    upperCaseWorks = on
    worksAsWell = Enabled

    [Advanced]
    arrayWorkToo =
        arrayElement1
        arrayElement2
    valueLessKey",
    new ConfigParserSettings
    {
        MultiLineValues = MultiLineValues.Simple | MultiLineValues.AllowValuelessKeys | MultiLineValues.QuoteDelimitedValues,
        Culture = new CultureInfo("en-US")
    }
);

configFileFromString.GetValue("Strings", "canBeIndented");          // value
configFileFromString["Strings"]["canBeIndented"];                   // returns 'value' too
configFileFromString.GetValue("Strings", "andQuoted");              // quotes will be stripped

configFileFromString.GetValue("Numbers", "withD", 0D);              // 0,6
configFileFromString.GetValue("Numbers", "dollars", 0D,             // 2999
    NumberStyles.AllowCurrencySymbol);
configFileFromString.GetValue("Numbers", "dollars");                // $2,999

configFileFromString.GetValue("boolean", "numericTrue", false);     // True
configFileFromString.GetValue("boolean", "textFalse", false);       // True
configFileFromString.GetValue("boolean", "yesWorks", false);        // True
configFileFromString.GetValue("boolean", "upperCaseWorks", false);  // True
configFileFromString.GetValue("boolean", "worksAsWell", false);     // True

configFileFromString.GetArrayValue("Advanced", "arraysWorkToo");     // ["arrayElement1","arrayElement2"]
configFileFromString.GetValue("Advanced", "valueLessKey");          //

How to build

You need Git and .NET Core SDK

git clone https://github.com/salaros/ConfigParser
cd ConfigParser
dotnet build

How to test

ConfigParser uses xUnit.net, so you can run unit tests by simply using the following command:

dotnet test tests

License

ConfigParser is distributed under the MIT license, which grants you

  • Private use
  • Commercial use
  • Modification
  • Distribution

However you have to include the content of license file in your source code (if you distribute your Software in text form), otherwise include it in your own LICENSE file or to some sort of About -> Open-source libraries section if you distribute your Software as a compiled library / binary. Here is why (part of MIT license):

The above copyright notice and this permission notice 
shall be included in all copies or substantial portions of the Software.