Files
ShrlAlgoToolkit/NeuWPF/NeoUI/Appearance/ThemePreferenceStore.cs
ShrlAlgo 955a01f564 整理
2025-08-20 12:10:35 +08:00

58 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
namespace NeoUI.Appearance
{
/// <summary>
/// 主题偏好存储:简单文本文件:第一行 ThemeMode第二行 ThemePalette
/// 文件位置:%LOCALAPPDATA%\{LibraryNamespace}\theme.pref
/// </summary>
internal static class ThemePreferenceStore
{
private static readonly string Folder =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
ThemeManager.LibraryNamespace ?? "AppTheme");
private static readonly string FilePath = Path.Combine(Folder, "theme.pref");
public static void Save(ThemeMode themeMode, ThemePalette palette)
{
try
{
if (!Directory.Exists(Folder))
Directory.CreateDirectory(Folder);
File.WriteAllLines(FilePath, new[] { themeMode.ToString(), palette.ToString() });
}
catch
{
// 忽略写入异常
}
}
/// <summary>
/// 读取偏好;成功返回 true否则 false。不存在或解析失败不抛异常。
/// </summary>
public static bool TryLoad(out ThemeMode? mode, out ThemePalette? palette)
{
mode = null;
palette = null;
try
{
if (!File.Exists(FilePath))
return false;
var lines = File.ReadAllLines(FilePath);
if (lines.Length > 0 && Enum.TryParse(lines[0], true, out ThemeMode m))
mode = m;
if (lines.Length > 1 && Enum.TryParse(lines[1], true, out ThemePalette p))
palette = p;
return mode.HasValue || palette.HasValue;
}
catch
{
return false;
}
}
}
}