47 lines
1.6 KiB
C#
47 lines
1.6 KiB
C#
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 { /* 忽略保存失败 */ }
|
||
}
|
||
}
|