It will be more convenient to store configuration in xml format, because you have options to store more complex value if you need to do so later. And also you can rely on XDocument
for loading and saving the file as simple as XDocument.Load(path)
and doc.Save(path)
. For example, your current configuration file will look like following in xml format :
<?xml version="1.0" encoding="utf-8"?>
<params>
<param name="param0">value_of_it</param>
<param name="param1">value_of_it</param>
<param name="something_else">you_get_it</param>
</params>
And your function to alter existing param's value or add new parameter to configuration file will be like so :
internal void alterXml(string param, bool replace = false, string value = null)
{
//load xml configuration file to XDocument object
var doc = XDocument.Load(path);
//search for <param> having attribute "name" = param
var existingParam = doc.Descendants("param").FirstOrDefault(o => o.Attribute("name").Value == param);
//if such a param element doesn't exist, add new element
if (existingParam == null)
{
var newParam = new XElement("param");
newParam.SetAttributeValue("name", param);
newParam.Value = "" + value;
doc.Root.Add(newParam);
}
//else update element's value
else if (replace) existingParam.Value = "" + value;
//save modified object back to xml file
doc.Save(path);
}