Skip to content

Commit 6887c35

Browse files
committed
More resilient config handling
1 parent 3edcf8e commit 6887c35

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

IPBanCore/Core/IPBan/IPBanConfig.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ public static void ChangeConfigAppSetting(XmlDocument doc, string key, string ne
659659
{
660660
newValue ??= string.Empty;
661661

662-
XmlNode appSettings = doc.SelectSingleNode($"/configuration/appSettings") ?? throw new InvalidOperationException("Unable to find appSettings in config");
663662
XmlNode existingSetting = doc.SelectSingleNode($"/configuration/appSettings/add[@key='{key}']");
664663

665664
if (existingSetting is null)
@@ -671,6 +670,20 @@ public static void ChangeConfigAppSetting(XmlDocument doc, string key, string ne
671670
XmlAttribute valueAttr = doc.CreateAttribute("value");
672671
valueAttr.Value = newValue;
673672
existingSetting.Attributes.Append(valueAttr);
673+
674+
XmlNode appSettings = doc.SelectSingleNode($"/configuration/appSettings");
675+
676+
// if not found, create appSettings node
677+
if (appSettings is null)
678+
{
679+
var configuration = doc.SelectSingleNode("/configuration");
680+
if (configuration is null)
681+
{
682+
configuration = doc.PrependChild(doc.CreateElement("configuration"));
683+
}
684+
appSettings = configuration.AppendChild(doc.CreateElement("appSettings"));
685+
686+
}
674687
appSettings.AppendChild(existingSetting);
675688
}
676689
else

0 commit comments

Comments
 (0)