public void EditSection(string sectionName, List<KeyValuePair<string, string>> properties)
{
EnsureSectionExists(sectionName);
// build dictionary from list
// ensure the keys will be looked up case-insensitive
var propertiesLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var pair in properties)
{
propertiesLookup.Add(pair.Key, pair.Value);
}
var lineNumber = 0;
if (TrySeekSection(sectionName, ref lineNumber))
{
lineNumber++;
string propertyName;
string propertyValue;
while (SeekProperty(ref lineNumber, out propertyName, out propertyValue))
{
var propertyDeleted = false;
if (propertiesLookup.ContainsKey(propertyName))
{
if (!string.Equals(propertiesLookup[propertyName], propertyValue))
{
if (propertiesLookup[propertyName] == null)
{
// delete the line
Lines.RemoveAt(lineNumber);
propertyDeleted = true;
}
else
{
// update the line
Lines[lineNumber] = propertyName + keyValueSeparator + propertiesLookup[propertyName];
}
}
propertiesLookup.Remove(propertyName);
}
if (!propertyDeleted)
{
lineNumber++;
}
}
foreach (var pair in properties)
{
if (propertiesLookup.ContainsKey(pair.Key))
{
Lines.Insert(lineNumber++, pair.Key + keyValueSeparator + pair.Value);
}
}
}
}