Files
qhmes/yy-admin-master/YY.Admin.Services/Service/Print/PrintDotSettings.cs

47 lines
1.6 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.
namespace YY.Admin.Services.Service.Print;
/// <summary>
/// 运行时可修改的 PrintDot 连接设置由设置页写入PrintDotService 优先读取)。
/// </summary>
public class PrintDotSettings
{
private static readonly string SettingsPath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"YY.Admin", "print-dot-settings.json");
public string WsUrl { get; set; } = "ws://127.0.0.1:1122/ws";
public string SelectedPrinter { get; set; } = string.Empty;
private static PrintDotSettings? _current;
public static PrintDotSettings? Current => _current;
public static PrintDotSettings Load()
{
try
{
if (System.IO.File.Exists(SettingsPath))
{
var json = System.IO.File.ReadAllText(SettingsPath);
_current = System.Text.Json.JsonSerializer.Deserialize<PrintDotSettings>(json) ?? new PrintDotSettings();
return _current;
}
}
catch { /* 读取失败使用默认值 */ }
_current = new PrintDotSettings();
return _current;
}
public void Save()
{
try
{
var dir = System.IO.Path.GetDirectoryName(SettingsPath)!;
System.IO.Directory.CreateDirectory(dir);
var json = System.Text.Json.JsonSerializer.Serialize(this, new System.Text.Json.JsonSerializerOptions { WriteIndented = true });
System.IO.File.WriteAllText(SettingsPath, json);
_current = this;
}
catch { /* 忽略保存失败 */ }
}
}