Tevux.Storage.Settings
2.1.0
dotnet add package Tevux.Storage.Settings --version 2.1.0
NuGet\Install-Package Tevux.Storage.Settings -Version 2.1.0
<PackageReference Include="Tevux.Storage.Settings" Version="2.1.0" />
paket add Tevux.Storage.Settings --version 2.1.0
#r "nuget: Tevux.Storage.Settings, 2.1.0"
// Install Tevux.Storage.Settings as a Cake Addin #addin nuget:?package=Tevux.Storage.Settings&version=2.1.0 // Install Tevux.Storage.Settings as a Cake Tool #tool nuget:?package=Tevux.Storage.Settings&version=2.1.0
Tevux-Software-Settings
For many years, we've been using File.WriteAllText
and File.ReadAllText
methods to store program settings in file.
However, every once in a while, when debugging client's broken systems, we find settings file completely empty. It is a
complete mystery of why this happens, but it happens nevertheless and thus we decided to build a reliable multi-stage
write process that at least guarantees us a valid settings file. Sure, if the power is cut during the write, newest
changes will be lost, but then at least original file will be restored automatically.
ReliableFile
Let's take settings.txt
file as an example. To read its content, use either of these methods:
public bool TryReadAllText(out string fileContent)
public bool TryReadAllBytes(out byte[] fileContent)
Since read operations are inherently quite safe, not much is happening behind the scenes. It is just a silent wrapper
around File.ReadAllBytes
which does not throw any exceptions.
The story is different for write counterparts:
public bool TryWriteAllBytes(byte[] bytesToWrite)
public bool TryWriteAllText(string textToWrite)
When calling those it goes like this:
- New content is written to
settings.txt.rf1
settings.txt.rf1
is renamed tosettings.txt.rf2
settings.txt
is deletedsettings.txt.rf2
is renamed tosettings.txt
Each step, starting from #2, validates the previous one; so, if the application crashed during file write, there are
quite a few good oppurtinities to restore the file - at least to the original content. This is done automatically when
calling Initialize
method.
Here's a basic usage example:
var reliableFile = new ReliableFile();
reliableFile.Initialize("temp/someFile.txt");
if (reliableFile.TryReadAllText(out var fileContent)){
Debug.Write(fileContent);
};
SettingsProvider
This class provides dictionary-like settings storage and utilizes ReliableFile
. It is simplistic by design. It only
allows storing very simple data types: int
, float
, double
, string
, bool
and DateTime
. Also, there are no
sections or categories. It is meant to store a handful of settings, and do that reliably.
Content is saved as JSON.
Here's how to use it:
var settingsFile = new ReliableFile();
settings.Initialize(settingsFile);
var settings = new SettingsProvider();
settingsFile.Initialize("temp/someSettings.json");
settings.TrySet("SomeIntegerKey", 123);
settings.TryGet("SomeIntegerKey", out int someIntegerValue);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- Utf8Json (>= 1.3.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.1.0 | 36 | 2/12/2025 |