Table of Contents

Method LoadFromFile

Namespace
LMKit.Data
Assembly
LM-Kit.NET.dll

LoadFromFile(string, bool, CancellationToken)

Loads a DataSource from the specified file path by lazily loading its content. This method opens the file in either read-only or read-write mode, and when read-write mode is used, the backing file remains open so that the DataSource is only partially loaded into memory with additional content decoded on demand during the instance's life cycle. This lazy-loading approach is recommended for large data sources.

public static DataSource LoadFromFile(string path, bool readOnly, CancellationToken cancellationToken = default)

Parameters

path string

The path to the file from which to load the data source.

readOnly bool

A flag indicating whether to open the file in read-only mode. If true, the file is opened as read-only; if false, the file is opened with read-write access, allowing the in-memory DataSource to persist changes back to the file.

cancellationToken CancellationToken

A token to monitor for cancellation requests during the asynchronous operation.

Returns

DataSource

A DataSource instance that lazily loads its content from the file.

Examples

using LMKit.Data;
using System;

// Load a data source in read-only mode
DataSource readOnlyDs = DataSource.LoadFromFile("knowledge-base.lmkds", readOnly: true);
Console.WriteLine($"Loaded: {readOnlyDs.Identifier} with {readOnlyDs.Sections.Count} sections");

// Load a data source in read-write mode (allows modifications)
DataSource editableDs = DataSource.LoadFromFile("knowledge-base.lmkds", readOnly: false);
// Changes will be persisted back to the file

Exceptions

ArgumentNullException

Thrown if path is null, empty, or consists solely of white-space characters.

IOException

Thrown if an I/O error occurs during file access.