Compare commits

...

2 Commits

Author SHA1 Message Date
7cea589294 test: 删除配置项示例 2025-01-21 19:23:57 +08:00
ad15422700 feat: 添加删除配置项的功能 2025-01-21 19:23:47 +08:00
2 changed files with 28 additions and 0 deletions

View File

@@ -106,4 +106,30 @@ public class Config
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json);
}
public void Delete(string key)
{
LoadConfigData();
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
var currentKey = keys[i];
if (!currentData.TryGetValue(currentKey, out var value)) return;
if (value is JObject jObject)
currentData[currentKey] =
jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
currentData = (Dictionary<string, object>)currentData[currentKey];
}
var finalKey = keys[^1];
currentData.Remove(finalKey);
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json);
}
}

View File

@@ -17,6 +17,8 @@ internal static class Program
var b = config.Get("aaa.bbb", "1");
var c = config.Get("aaa.sss", "1");
var d = config.Get("bbb.sss", "1");
config.Delete("aaa.www");
}
}