203 lines
5.7 KiB
C#
203 lines
5.7 KiB
C#
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();
|
|
|
|
/// <summary>
|
|
/// 初始化JsonConfig
|
|
/// </summary>
|
|
/// <param name="filePath">JSON文件路径</param>
|
|
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("创建了新配置文件");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置配置值(存在则覆盖)
|
|
/// </summary>
|
|
public void SetValue(string key, object value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
throw new ArgumentException("键名不能为空");
|
|
|
|
lock (js_fileLock)
|
|
{
|
|
js_configData[key] = JToken.FromObject(value);
|
|
SaveToFile();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置值
|
|
/// </summary>
|
|
public T GetValue<T>(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<T>();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"配置值转换失败,返回默认值: {ex.Message}");
|
|
return defaultValue;
|
|
}
|
|
}
|
|
return defaultValue;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有配置键值对
|
|
/// </summary>
|
|
public Dictionary<string, object> GetAllValues()
|
|
{
|
|
//lock (js_fileLock)
|
|
//{
|
|
// return JsonConvert.SerializeObject(js_configData, Formatting.Indented);
|
|
//}
|
|
return js_configData.ToObject<Dictionary<string, object>>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查配置键是否存在
|
|
/// </summary>
|
|
public bool ContainsKey(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return false;
|
|
|
|
lock (js_fileLock)
|
|
{
|
|
return js_configData.ContainsKey(key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除指定配置键
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存配置到文件
|
|
/// </summary>
|
|
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<string>("ServerIP");
|
|
int port = config.GetValue<int>("Port", 5020); // 带默认值
|
|
bool enabled = config.GetValue<bool>("Enabled");
|
|
|
|
// 获取所有配置
|
|
var allSettings = config.GetAllValues();
|
|
foreach (var setting in allSettings)
|
|
{
|
|
Console.WriteLine($"{setting.Key}: {setting.Value}");
|
|
}
|
|
|
|
*/ |