85 lines
2.0 KiB
C#
85 lines
2.0 KiB
C#
using System.Text.Json;
|
|
using ZA.CoreService.ESBCertificateManager.Models;
|
|
|
|
namespace ZA.CoreService.ESBCertificateManager.Services;
|
|
|
|
public sealed class LocalSetupSelectionStore
|
|
{
|
|
private static readonly JsonSerializerOptions JsonOptions =
|
|
new()
|
|
{
|
|
WriteIndented = true
|
|
};
|
|
|
|
private readonly string _filePath;
|
|
|
|
public LocalSetupSelectionStore()
|
|
{
|
|
string directory = Path.Combine(
|
|
Environment.GetFolderPath(
|
|
Environment.SpecialFolder.LocalApplicationData),
|
|
"ZA.CoreService.ESBCertificateManager");
|
|
|
|
_filePath = Path.Combine(
|
|
directory,
|
|
"setup-selection.json");
|
|
}
|
|
|
|
public LocalSetupSelection? Load()
|
|
{
|
|
if (!File.Exists(_filePath))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
try
|
|
{
|
|
string json =
|
|
File.ReadAllText(_filePath);
|
|
|
|
return JsonSerializer.Deserialize<LocalSetupSelection>(
|
|
json,
|
|
JsonOptions);
|
|
}
|
|
catch
|
|
{
|
|
// Eine beschädigte lokale Auswahl darf den Start
|
|
// nicht verhindern. Das Setup wird erneut angezeigt.
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void Save(
|
|
LocalSetupSelection selection)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(selection);
|
|
|
|
string? directory =
|
|
Path.GetDirectoryName(_filePath);
|
|
|
|
if (string.IsNullOrWhiteSpace(directory))
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Der lokale Setup-Ordner konnte nicht bestimmt werden.");
|
|
}
|
|
|
|
Directory.CreateDirectory(directory);
|
|
|
|
string json =
|
|
JsonSerializer.Serialize(
|
|
selection,
|
|
JsonOptions);
|
|
|
|
File.WriteAllText(
|
|
_filePath,
|
|
json);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
if (File.Exists(_filePath))
|
|
{
|
|
File.Delete(_filePath);
|
|
}
|
|
}
|
|
} |