49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System.IO;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace FoxBrowser.Core;
|
|
|
|
public sealed class AppSettings
|
|
{
|
|
public string AppDisplayName { get; set; } = "FoxBrowser";
|
|
public string? AppIconPath { get; set; }
|
|
}
|
|
|
|
public static class AppSettingsStore
|
|
{
|
|
public static string SettingsPath => Path.Combine(AppContext.BaseDirectory, "settings.json");
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new()
|
|
{
|
|
WriteIndented = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.Never
|
|
};
|
|
|
|
public static AppSettings Load()
|
|
{
|
|
try
|
|
{
|
|
if (!File.Exists(SettingsPath)) return new AppSettings();
|
|
var settings = JsonSerializer.Deserialize<AppSettings>(File.ReadAllText(SettingsPath), JsonOptions);
|
|
if (settings is null) return new AppSettings();
|
|
if (string.IsNullOrWhiteSpace(settings.AppDisplayName)) settings.AppDisplayName = "FoxBrowser";
|
|
if (!string.IsNullOrWhiteSpace(settings.AppIconPath) && !File.Exists(settings.AppIconPath))
|
|
settings.AppIconPath = null;
|
|
return settings;
|
|
}
|
|
catch
|
|
{
|
|
return new AppSettings();
|
|
}
|
|
}
|
|
|
|
public static void Save(AppSettings settings)
|
|
{
|
|
Directory.CreateDirectory(AppContext.BaseDirectory);
|
|
var tempPath = SettingsPath + ".tmp";
|
|
File.WriteAllText(tempPath, JsonSerializer.Serialize(settings, JsonOptions));
|
|
File.Move(tempPath, SettingsPath, overwrite: true);
|
|
}
|
|
}
|