Skip to content
github-actions[bot] edited this page Apr 6, 2026 · 2 revisions

Reloading

IniConfig.Reload() re-applies the full Loading-Life-Cycle (steps 1–6) in place, updating the property values of the already-registered section objects without creating new instances.


Singleton guarantee

GetSection<T>() always returns the same object reference, even after Reload().

The framework updates the properties of the existing section object during a reload, so any code that holds a reference to the section will automatically see the new values without re-querying the registry.


Triggering a reload

// Explicitly trigger a reload at any time:
config.Reload();

// Async reload — does not block the calling thread:
await config.ReloadAsync(cancellationToken);

// React to the reload completing (fires after both Reload() and ReloadAsync()):
config.Reloaded += (sender, _) =>
    Console.WriteLine($"{((IniConfig)sender!).FileName} was reloaded.");

Automatic reload on file change

Use MonitorFile() on the builder to install a FileSystemWatcher that triggers Reload() automatically when the file changes on disk. See File-Change-Monitoring for the full callback API.


Change tracking

IniConfig.HasPendingChanges() returns true when at least one registered section has been modified since the last load or save.

if (config.HasPendingChanges())
    config.Save();

// Per-section dirty flag:
var section = config.GetSection<IAppSettings>();
if (section.HasChanges)
    Console.WriteLine("Section has unsaved changes.");

Both flags are cleared automatically by Reload() (after fresh data is applied) and by Save() (after a successful write).

Manually signalling a change — MarkAsDirty()

Property setters set the dirty flag automatically. However, in-place collection mutations (e.g. section.Tags.Add("item")) bypass the setter and therefore do not trigger the flag. Call MarkAsDirty() after such mutations so that HasPendingChanges() and the auto-save timer detect the change:

// Mutation bypasses the setter — auto-save won't notice without MarkAsDirty()
section.Tags.Add("new-tag");

// Explicitly mark the section as dirty:
section.MarkAsDirty();

// Now HasPendingChanges() returns true and the auto-save timer will pick it up.

See also

Clone this wiki locally