NTDLS.Persistence Usage
Helpers for saving and loading serialized objects to and from files, ideal for application configuration.
NTDLS.Persistence
What it is. A small set of helpers for reading and writing serialized objects to and from disk. The headline use case is application configuration: save a plain C# object once, load it back next run, with no JSON/XML mapping or file-path bookkeeping.
When to reach for it. You want per-machine settings (last window size, chosen options, cached credentials) persisted with as little code as possible. Skip it if you need portable, human-editable config that travels between machines or versions - a JSON file you control gives you more flexibility.
Getting started
Define any serializable class, then save and load it by program name:
class UIPreferences
{
public string Username { get; set; } = string.Empty;
public bool RememberPassword { get; set; }
}
static void Main()
{
// Saving preferences:
var preferences = new UIPreferences
{
Username = "JPatterson",
RememberPassword = false
};
// The file is named after the class type plus the program name,
// and stored in the common programs data directory.
CommonApplicationData.SaveToDisk("MyProgramName", preferences);
// Loading them back later:
var loadedPreferences = CommonApplicationData.LoadFromDisk<UIPreferences>("MyProgramName");
Console.WriteLine($"Welcome back, {loadedPreferences.Username}!");
}
The generic argument on LoadFromDisk tells the helper which shape to reconstruct, so each setting class round-trips independently under the same program name.
NuGet: NTDLS.Persistence