rskibbe.Ini – Parsing, reading & saving INI files in .NET – but easy!

rskibbe.Ini - Working with INI files - but easy
rskibbe.Ini – Working with INI files – but easy

What is rskibbe.Ini?

A simple package for work regarding the INI format: Parsing from/to string and creating and saving ini files – but easy!

💡 In a hurry?: No problem, just navigate to the points of interest by using the table of contents from above. If you need a quick example, or an example download, scroll to the bottom.

Quick example

Quick example for rskibbe.Ini parsing, reading & saving INI files - but easy
Quick example for rskibbe.Ini parsing, reading & saving INI files – but easy

Here’s a quick example without talking to much, go into the specific sections of this article to get more details.

Step 1 – Install the package

Open the NuGet Package Manager and execute the following commands (or install it by GUI):

Install-Package rskibbe.Ini

Step 2 – Creating an INI instance

Next, you can create a simple INI by providing some information as shown below. Keep in mind, that „INI“ is roughly / only a format which I personally don’t directly associate with a „file“. For me, it’s primarily a format which displays a „parent“ with a set of child „sections“.

You could for example parse a string from memory or even from a webservice (whoever does this.. but „stay closed for modification and open for extension“ is the thing, right?). The sections can have multiple entries, which are basically key & value pairs.

// parsing from string
var ini = rskibbe.Ini.Models.Ini.FromString("[FtpSettings]user=someuser\r\npassword=mypw", "\r\n");

// manual step by step creation
var ini = rskibbe.Ini.Models.Ini
    // using the "Factory Property"
    .New
    // create the first section and..
    .AddSection("NetworkSettings", section =>
    {
        // use an anonymous action with Section argument to register entries
        section
            // register the first entry
            .AddEntry("host", "127.0.0.1")
            // ...
            .AddEntry("user", "networkuser");
    })
    // being able to chain and keeping a nice fluent api
    .AddSection("FtpSettings", section =>
    {
        section
            .AddEntry("user", "ftpuser")
            .AddEntry("password", "ftppw");
    });

Step 3 – Transform from/to an INI file

You can for example transform the above from/into an INI file and save it to disc with the following code:

Transforming from Ini instance

// loading from file
var iniFile = await IniFile.LoadAsync(".\\settings.ini");

// creating an instance
var iniFile = ini.ToFile(".\\settings.ini");
// now save that file
await iniFile.SaveAsync();

Directly creating an IniFile instance

var iniFile = IniFile
    .New
    .AddSection("NetworkSettings", section =>
    {
        // use an anonymous action with Section argument to register entries
        section
        // register the first entry
            .AddEntry("host", "127.0.0.1")
            // ...
            .AddEntry("user", "networkuser");
    })
    .AddSection("FtpSettings", section =>
    {
        section
            .AddEntry("user", "ftpuser")
            .AddEntry("password", "ftppw");
    });

Step 4 – Getting values from the INI File

// step by step
var ftpSettingsSection = ini["FtpSettings"];
var ftpUser = ftpSettingsSection["user"];
var ftpPassword = ftpSettingsSection["password"];

// in one line
var ftpPassword = ini["FtpSettings"]["user"];

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert