MetaforceInstaller/MetaforceInstaller.Core/Services/StorageService.cs

36 lines
No EOL
1 KiB
C#

using System.Text.Json;
using MetaforceInstaller.Core.Intefaces;
using MetaforceInstaller.Core.Models;
namespace MetaforceInstaller.Core.Services;
public class StorageService : IStorageService
{
private readonly string _storagePath;
public StorageService()
{
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var appDirectory = Path.Combine(appData, "MetaforceInstaller");
Directory.CreateDirectory(appDirectory);
_storagePath = Path.Combine(appDirectory, "installations.json");
}
public AppData Load()
{
if (!File.Exists(_storagePath))
return new AppData();
var json = File.ReadAllText(_storagePath);
return JsonSerializer.Deserialize<AppData>(json) ?? new AppData();
}
public void Save(AppData data)
{
var json = JsonSerializer.Serialize(data, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(_storagePath, json);
}
}