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 = [];
	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); also when disposing and when the process exits.

NoAutoSaveTimer

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

Methods

Name Description
Dispose()

Saves now if need, and releases used resources. In the future will not save or reload. Don't need to call if the settings are used until process exit.

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

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

Reload<T>(out T)

Reloads settings from the file.

SaveIfNeed()

Saves now if need. Call this when you want to save changed settings immediately; else the changes are auto-saved after max 2 s. See also JSettings.NoAutoSave and JSettings.NoAutoSaveTimer.

Events

Name Description
ModifiedExternally

When detected that the settings file was externally modified or deleted (for example by another process of your program).