feat: 添加删除配置项的功能
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user