Show / Hide Table of Contents

Class JSettings

Base of record classes that contain various settings as public fields. Loads and lazily auto-saves from/to a JSON file.

public abstract record JSettings : IDisposable, IEquatable<JSettings>
Remarks

All functions are thread-safe.

Examples
MySettings sett = MySettings.Load(); //in a class you would use a static field or property, but this example uses a local variable for simplicity

print.it(sett.i);
sett.i++;

if (dialog.showInput(out string s, "example", editText: sett.s)) {
	sett.s = s;
	
	print.it("old array:", sett.a);
	if ((!sett.a.Contains(s))) sett.a = sett.a.InsertAt(-1, s);
	
	print.it("old dictionary:", sett.d);
	sett.d[s] = DateTime.Now;
}


record class MySettings : JSettings {
	public static readonly string File = folders.ThisAppDocuments + @"MySettings.json";

	public static MySettings Load() => Load<MySettings>(File);
	
	// examples of settings
	public int i;
	public string s = "default";
	public string[] a = Array.Empty<string>();
	public Dictionary<string, DateTime> d = new();
}

Namespace: Au.Types
Assembly: Au.dll
Inheritance
object
JSettings

Properties

Name Description
LoadedFile

true if settings were loaded from file.

NoAutoSave

Don't automatically call JSettings.SaveIfNeed. If false (default), calls every 2 s (unless JSettings.NoAutoSaveTimertrue), when disposing, and when process exits.

NoAutoSaveTimer

Don't call JSettings.SaveIfNeed every 2 s. Default false.

Methods

Name Description
Dispose()

Call this when finished using the settings. Saves now if need, and stops autosaving. Don't need to call if the settings are used until process exit.

Dispose(bool)
Load<T>(string, bool)

Loads a JSON file and deserializes to an object of type T, or creates a new object of type T.

SaveIfNeed()

Saves now if need. Don't need to call explicitly. Autosaving is every 2 s, also on process exit and Dispose.