using System; using System.Collections.Generic; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace C_Windows_1 { internal class JsonConfig // Json文件类 (最底部有使用示例) { private readonly string js_filePath; private JObject js_configData; private readonly object js_fileLock = new object(); /// /// 初始化JsonConfig /// /// JSON文件路径 public JsonConfig(string filePath) { if (string.IsNullOrWhiteSpace(filePath)) throw new ArgumentException("文件路径不能为空或空白"); js_filePath = Path.GetFullPath(filePath); //Console.WriteLine($"配置文件路径: {js_filePath}"); EnsureDirectoryExists(); LoadOrCreateConfig(); } private void EnsureDirectoryExists() { var directory = Path.GetDirectoryName(js_filePath); if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } } private void LoadOrCreateConfig() { lock (js_fileLock) { if (File.Exists(js_filePath)) { try { string json = File.ReadAllText(js_filePath); js_configData = JObject.Parse(json); //Console.WriteLine("配置加载成功"); return; } catch (Exception ex) { Console.WriteLine($"配置加载失败,将创建新配置: {ex.Message}"); } } js_configData = new JObject(); SaveToFile(); Console.WriteLine("创建了新配置文件"); } } /// /// 设置配置值(存在则覆盖) /// public void SetValue(string key, object value) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键名不能为空"); lock (js_fileLock) { js_configData[key] = JToken.FromObject(value); SaveToFile(); } } /// /// 获取配置值 /// public T GetValue(string key, T defaultValue = default) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException("键名不能为空"); lock (js_fileLock) { if (js_configData.TryGetValue(key, out JToken token)) { try { return token.ToObject(); } catch (Exception ex) { Console.WriteLine($"配置值转换失败,返回默认值: {ex.Message}"); return defaultValue; } } return defaultValue; } } /// /// 获取所有配置键值对 /// public Dictionary GetAllValues() { //lock (js_fileLock) //{ // return JsonConvert.SerializeObject(js_configData, Formatting.Indented); //} return js_configData.ToObject>(); } /// /// 检查配置键是否存在 /// public bool ContainsKey(string key) { if (string.IsNullOrWhiteSpace(key)) return false; lock (js_fileLock) { return js_configData.ContainsKey(key); } } /// /// 删除指定配置键 /// public bool RemoveKey(string key) { if (string.IsNullOrWhiteSpace(key)) return false; lock (js_fileLock) { bool removed = js_configData.Remove(key); if (removed) { SaveToFile(); } return removed; } } public string ShowAllValues() { return JsonConvert.SerializeObject(js_configData, Formatting.Indented); } /// /// 保存配置到文件 /// private void SaveToFile() { try { string json = JsonConvert.SerializeObject(js_configData, Formatting.Indented); File.WriteAllText(js_filePath, json); //Console.WriteLine("配置保存成功"); } catch (Exception ex) { Console.WriteLine($"配置保存失败: {ex.Message}"); throw; // 可以根据需要改为更温和的错误处理 } } } } /* * 使用示例 // 初始化配置 var config = new JsonConfig("config.json"); // 设置值(自动创建文件) config.SetValue("ServerIP", "192.168.1.100"); config.SetValue("Port", 502); config.SetValue("Enabled", true); // 读取值 string ip = config.GetValue("ServerIP"); int port = config.GetValue("Port", 5020); // 带默认值 bool enabled = config.GetValue("Enabled"); // 获取所有配置 var allSettings = config.GetAllValues(); foreach (var setting in allSettings) { Console.WriteLine($"{setting.Key}: {setting.Value}"); } */