icfg-section.md 1.1 KB

ICfgSection

Configuration section interface for accessing sub-trees.

Properties

Path

The full path of this section.

string Path { get; }

Methods

Get

Get a value relative to this section.

string? Get(string key)
T? Get<T>(string key)

Set

Set a value relative to this section.

void Set(string key, string? value, int? targetLevel = null)

GetSection

Get a child section.

ICfgSection GetSection(string key)

GetChildKeys

Get all child keys.

IEnumerable<string> GetChildKeys()

Example

var dbSection = cfg.GetSection("Database");

// Read values
var host = dbSection.Get("Host");
var port = dbSection.Get<int>("Port");

// Nested section
var connSection = dbSection.GetSection("Connection");
var timeout = connSection.Get<int>("Timeout");

// Enumerate children
foreach (var key in dbSection.GetChildKeys())
{
    Console.WriteLine($"{key}: {dbSection.Get(key)}");
}

Next Steps