Compare commits
2 Commits
6e3450fef2
...
7cea589294
Author | SHA1 | Date | |
---|---|---|---|
7cea589294
|
|||
ad15422700
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user