If you often find yourself creating standalone projects that need to save Global app information for example form settings, maybe this article will be helpful. I often find myself reinventing the wheel, instead of using this class for reading writing and updating a key - value.
Introduction
Saving to a Key-Value file is a simple and useful operation for any type of software, although, from my experience, you can easily run into agility problems when retrieving this information.
Using this class and with little effort, you can view, create, modify a key. Using LINQ (one of the wonders of the world), you can search for a specific key or display all of them.
A boon for anyone beginning to write code.
Background
You can add this simple class into your project and immediately start saving the information to a Json format file disguising it with any extension.
Once you've inserted this class, you'll need to include the Newtonsoft.Json nuget
library with a simple CTRL+.
Then, using Linq and with very little effort, you can read all the information one by one or all at once.
Using the Code
So let's start: in our project, we insert a new class:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SettingsFileClass
{
public class SettingsInJsonFile
{
public const string FilePath = @"D:\temp\settings.json";
public const Formatting MyFormatting = Formatting.Indented;
internal string _key;
public string Key { get { return _key; } set { _key = value.ToUpper(); } }
public string? Value { get; set; }
internal static bool Set(string Key, string NewValue, bool CreateIfNew = false)
{
Key = Key.ToUpper();
var Item = GetObject(Key);
var _data = GetAll().ToList();
if (Item == null)
{
if (CreateIfNew)
{
_data.Add(new SettingsInJsonFile()
{
Key = Key,
Value = NewValue
});
}
else
{
return false;
}
}
else
_data.First(x => x.Key == Key).Value = NewValue;
string json = JsonConvert.SerializeObject(_data.ToArray(), MyFormatting);
File.WriteAllText(FilePath, json);
return true;
}
internal static bool Delete(string KeyToDelete)
{
if (Exist(KeyToDelete))
{
var _data = GetAll().Where
(x => x.Key != KeyToDelete.ToUpper()).ToArray();
string json = JsonConvert.SerializeObject(_data, MyFormatting);
File.WriteAllText(FilePath, json);
return true;
}
return false;
}
internal static List<SettingsInJsonFile> GetAll()
{
if (!File.Exists(FilePath))
{
using StreamWriter wr = File.CreateText(FilePath);
wr.WriteLine("[]");
}
using StreamReader r = new(FilePath);
string json = r.ReadToEnd();
if (string.IsNullOrEmpty(json)) json = "[]";
var Response = JsonConvert.DeserializeObject
<List<SettingsInJsonFile>>(json);
return Response;
}
internal static string GetValue(string Key) =>
GetAll().FirstOrDefault(x => x.Key == Key.ToUpper()).Value;
internal static SettingsInJsonFile? GetObject(string Key) =>
GetAll().FirstOrDefault(x => x.Key == Key.ToUpper());
internal static bool Exist(string Key) => GetAll().FirstOrDefault
(x => x.Key == Key.ToUpper()) != null;
}
}
The file result in the file is, for example:
[
{"Key":"key","Value":"07/10/2022 13:04:21"},
{"Key":"backcolor","Value":"#FFFFFF"}
{"Key":"forecolor","Value":"#000000"}
]
Points of Interest
This simple class is powerful enough for small text files used for local storage. I would not recommend it for more complex scenarios or where the number of variables begin to grow exponentially.
The function "CREATE
" create Keys, but these keys are Keysensitive
, so a good check to add would be to create all keys as uppercase only.
History
- 7th October, 2022: Initial version