You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1008 B

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace LocalDataStorageWithSQLite.StorageUtils
{
internal class FileSystemStorage
{
private string _dir = $"{FileSystem.AppDataDirectory}/Temp";
public FileSystemStorage()
{
if (!Directory.Exists(_dir))
Directory.CreateDirectory(_dir);
}
public T Get<T>(string key)
{
string path = Path.Combine(_dir, key);
if (!File.Exists(path))
return default(T);
string raw = File.ReadAllText(path);
T result = JsonSerializer.Deserialize<T>(raw);
return result;
}
public void Set<T>(string key, T value)
{
var serialized = JsonSerializer.Serialize(value);
string path = Path.Combine(_dir, key);
File.WriteAllText(path, serialized);
}
}
}