Table of Contents

IniDocument Class

Definition

Namespace
Bodu.Text.Configuration
Assembly
Bodu.Text.Configuration.dll
Package
Bodu.Text.Configuration 1.0.0
Source
IniDocument.cs

Represents a mutable INI-style document, providing access to the global section and all named sections, together with a mutation surface for authoring documents programmatically.

public sealed class IniDocument : IniDocumentBase
Inheritance
IniDocument
Inherited Members
Extension Methods

Examples

var doc = new IniDocument();
IniSection db = doc.GetOrAddSection("database");
db.SetEntry("host", "localhost");

// Walk every named section.
foreach (IniSection section in doc.Sections)
    Console.WriteLine($"[{section.Name}] - {section.Entries.Count} entries");

// Read the global section (keys that appeared before any header).
IniSection global = doc.GlobalSection;

// Lookup by section name.
if (doc.TryGetSection("database", out IniSection? found))
    Console.WriteLine(found!["host"]);

Remarks

Build an instance from scratch using the constructors and the AddSection(IniSection)/GetOrAddSection(string) members; a parsed document is obtained through Parse(string, ConfigurationParseOptions?), which exposes the same base read surface.

Section lookup uses ordinal comparison, case-insensitive by default and case-sensitive when the document was created with that option.

Constructors

IniDocument(IniSection, IEnumerable<IniSection>, bool)

Initializes a new instance of the IniDocument class from a global section and an ordered sequence of named sections. Use this constructor to build documents programmatically without first parsing configuration text.

public IniDocument(IniSection globalSection, IEnumerable<IniSection> sections, bool caseSensitiveSections = false)

Parameters

globalSection IniSection

The global section (entries authored before the first named section header). Its Name must be the empty string.

sections IEnumerable<IniSection>

The ordered, named sections that follow the global section.

caseSensitiveSections bool

true to compare section names with ordinal case sensitivity; otherwise, false (the INI default).

Exceptions

ArgumentNullException

Thrown when globalSection or sections is null.

ArgumentException

Thrown when globalSection has a non-empty Name, or when sections contains a null entry.

IniDocument(bool)

Initializes a new instance of the IniDocument class with an empty global section and no named sections.

public IniDocument(bool caseSensitiveSections = false)

Parameters

caseSensitiveSections bool

true to compare section names with ordinal case sensitivity; otherwise, false (the INI default).

Methods

AddSection(IniSection)

Appends section to the document.

public void AddSection(IniSection section)

Parameters

section IniSection

The section to append.

Exceptions

ArgumentNullException

section is null.

ArgumentException

section has an empty Name (i.e. it is the global section).

GetOrAddSection(string)

Returns the first section with the supplied name, creating and appending one when no match exists.

public IniSection GetOrAddSection(string name)

Parameters

name string

The section name.

Returns

IniSection

The matching or newly created section.

Exceptions

ArgumentNullException

name is null.

ArgumentException

name is empty.

RemoveSection(string)

Removes the section with the supplied name. When multiple sections share the same name (under Preserve or MergeAdjacent ), every occurrence is removed.

public bool RemoveSection(string name)

Parameters

name string

The section name to remove.

Returns

bool

true when at least one section was removed; otherwise, false.

Exceptions

ArgumentNullException

name is null.

Applies to

ProductVersions
.NET8, 10