add storage functionality and appdata model
This commit is contained in:
parent
b4ce64456e
commit
c29658e7e4
3 changed files with 51 additions and 0 deletions
36
MetaforceInstaller.Core/Services/StorageService.cs
Normal file
36
MetaforceInstaller.Core/Services/StorageService.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue