NTDLS.Persistence 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package NTDLS.Persistence --version 2.0.1
                    
NuGet\Install-Package NTDLS.Persistence -Version 2.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NTDLS.Persistence" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NTDLS.Persistence" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="NTDLS.Persistence" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NTDLS.Persistence --version 2.0.1
                    
#r "nuget: NTDLS.Persistence, 2.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NTDLS.Persistence@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NTDLS.Persistence&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=NTDLS.Persistence&version=2.0.1
                    
Install as a Cake Tool

NTDLS.Persistence

NTDLS.Persistence is a small .NET helper library (targets .NET 8, 9 and 10) for persisting objects to disk as JSON. It writes your objects into the standard Windows application-data folders — per-user local, roaming, or machine-wide common data — so you don't have to manage paths, directories, or serialization yourself. Files are named after the type being saved, live in a folder named after your application, and can optionally be encrypted at rest by supplying your own IEncryptionProvider.

📦 Be sure to check out the NuGet package: https://www.nuget.org/packages/NTDLS.Persistence

When to use this package

Use NTDLS.Persistence when you need simple file-based persistence without rolling your own path handling and JSON code, for example:

  • Saving configuration files — application settings, user preferences, connection strings, and similar state that should survive restarts.
  • Choosing the right storage location automatically — pick between per-user non-roaming data (LocalUserApplicationData), per-user roaming data (RoamingUserApplicationData, synced via roaming profiles), or machine-wide data shared by all users (CommonApplicationData) with a single class swap.
  • Encrypting persisted data at rest — pass any implementation of IEncryptionProvider to encrypt the serialized bytes before they touch disk and decrypt them on load.
  • Customizing serialization — an overload accepts JsonSerializerOptions (Newtonsoft.Json) when you need custom converters, naming strategies, or formatting.
  • Keeping things boringly simple — no database, no ORM, no registry: just one JSON file per type under %LOCALAPPDATA%, %APPDATA%, or the common program-data directory.

Usage examples

Saving and loading a settings object

The most common case: save an object to disk and load it back later. The file is stored as <TypeName>.json inside a folder named after your application in the chosen data directory.

using NTDLS.Persistence;

// The object to persist — any JSON-serializable type works.
class UIPreferences
{
    public string Username { get; set; } = string.Empty;
    public bool RememberPassword { get; set; }
}

static void Main()
{
    // Create some preferences.
    var preferences = new UIPreferences
    {
        Username = "JPatterson",
        RememberPassword = false
    };

    // Save to the current user's LocalAppData folder:
    //   %LOCALAPPDATA%\MyProgramName\UIPreferences.json
    LocalUserApplicationData.SaveToDisk("MyProgramName", preferences);

    // Load it back later. Returns null if the file does not exist yet.
    UIPreferences? loaded = LocalUserApplicationData.LoadFromDisk<UIPreferences>("MyProgramName");
}

Loading with a default value

If the file may not exist (first run), use the overload that takes a default result instead of dealing with null:

// Returns the provided default if no saved file exists.
var prefs = LocalUserApplicationData.LoadFromDisk(
    "MyProgramName",
    new UIPreferences()); // defaultResult

Choosing where the data lives

All three facades expose the same API — swap the class to change the storage location:

using NTDLS.Persistence;

// Per-user, non-roaming: %LOCALAPPDATA%\MyProgramName\...
LocalUserApplicationData.SaveToDisk("MyProgramName", settings);

// Per-user, roaming: %APPDATA%\MyProgramName\...
RoamingUserApplicationData.SaveToDisk("MyProgramName", settings);

// Machine-wide, all users: C:\ProgramData\MyProgramName\...
CommonApplicationData.SaveToDisk("MyProgramName", settings);

Custom JSON serialization settings

Pass JsonSerializerOptions when you need control over how the object is serialized:

using System.Text.Json;
using NTDLS.Persistence;

var options = new PersistenceOptions
{
    Json = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }
};

// Pass the same options when loading.
LocalUserApplicationData.SaveToDisk("MyProgramName", preferences, options);
var loaded = LocalUserApplicationData.LoadFromDisk<UIPreferences>("MyProgramName", options);

Encrypting persisted data

Implement IEncryptionProvider and set it on PersistenceOptions.Encryption. The provider receives the UTF-8 JSON bytes:

using NTDLS.Persistence;

// A minimal AES-based provider (your key management is up to you).
sealed class MyEncryptionProvider : IEncryptionProvider
{
    private readonly byte[] _key;
    private readonly byte[] _iv;

    public MyEncryptionProvider(byte[] key, byte[] iv)
    {
        _key = key;
        _iv = iv;
    }

    public byte[] Encrypt(byte[] plainTextBytes)
    {
        using var aes = System.Security.Cryptography.Aes.Create();
        aes.Key = _key;
        aes.IV = _iv;
        return aes.EncryptCbc(plainTextBytes);
    }

    public byte[] Decrypt(byte[] encryptedTextBytes)
    {
        using var aes = System.Security.Cryptography.Aes.Create();
        aes.Key = _key;
        aes.IV = _iv;
        return aes.DecryptCbc(encryptedTextBytes);
    }
}

// Use it on both save and load — the same provider must be used for both.
var options = new PersistenceOptions { Encryption = new MyEncryptionProvider(key, iv) };
LocalUserApplicationData.SaveToDisk("MyProgramName", preferences, options);
var loaded = LocalUserApplicationData.LoadFromDisk<UIPreferences>("MyProgramName", options);

Compression and OS protection

PersistenceOptions also takes a compression provider (BrotliCompressionProvider or DeflateCompressionProvider) and a Protect flag. Protect encrypts the data with the operating system (DPAPI on Windows, a per-user key file elsewhere) and stores it in a .bin file. On save the order is serialize, compress, encrypt, protect; load reverses it.

var options = new PersistenceOptions { Compression = new BrotliCompressionProvider(), Protect = true };
LocalUserApplicationData.SaveToDisk("MyProgramName", preferences, options);

Deleting a saved file

// Deletes %LOCALAPPDATA%\MyProgramName\UIPreferences.json
LocalUserApplicationData.DeleteFromDisk("MyProgramName", typeof(UIPreferences));

License

MIT

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on NTDLS.Persistence:

Package Downloads
NTDLS.Wv.Engine

Warped Vector is a 2D game engine written in C# and rendered with DirectX.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 51 10/31/2026
2.0.1 89 10/21/2026
2.0.0 88 10/21/2026
1.3.1 510 11/14/2025
1.3.0 437 3/4/2025
1.2.3 292 3/2/2025
1.2.2 348 12/31/2024
1.2.1 419 6/20/2024
1.2.0 347 1/22/2024
1.1.0 352 11/23/2023
1.0.2 287 11/20/2023
1.0.1 232 11/15/2023
1.0.0 288 11/3/2023

Dependency updates and introduced .net 10 support.