using System.Text.Json; namespace ShengShengBuXi.ConsoleApp.Models; /// /// 应用程序设置 /// public class AppSettings { /// /// SignalR Hub的URL /// public string SignalRHubUrl { get; set; } = "http://localhost:5000/phonehub"; /// /// 配置文件备份路径 /// public string ConfigBackupPath { get; set; } = "config.json"; /// /// 是否自动连接到服务器 /// public bool AutoConnectToServer { get; set; } = true; /// /// 是否允许离线启动 /// public bool AllowOfflineStart { get; set; } = false; /// /// 从文件加载设置 /// /// 文件路径 /// 应用程序设置 public static AppSettings LoadFromFile(string filePath) { if (!File.Exists(filePath)) { return new AppSettings(); } try { string json = File.ReadAllText(filePath); var settings = JsonSerializer.Deserialize(json); return settings ?? new AppSettings(); } catch (Exception ex) { Console.WriteLine($"加载应用程序设置失败: {ex.Message}"); return new AppSettings(); } } /// /// 保存设置到文件 /// /// 文件路径 public void SaveToFile(string filePath) { try { var options = new JsonSerializerOptions { WriteIndented = true }; string json = JsonSerializer.Serialize(this, options); File.WriteAllText(filePath, json); } catch (Exception ex) { Console.WriteLine($"保存应用程序设置失败: {ex.Message}"); } } }