internal void RemoveConfigurationSection(string group, string name) {
bool sectionIsUsed = false; // Is section used in our record
VerifySectionName(name, null, true);
string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);
// If it's already removed, don't try to remove it again.
if (RemovedSections.Contains(configKey)) {
return;
}
// If it's not a registered section, there's nothing to do.
if (FindFactoryRecord(configKey, true) == null) {
return;
}
// Detach from this record
ConfigurationSection configSection = GetConfigSection(configKey);
if (configSection != null) {
configSection.SectionInformation.DetachFromConfigurationRecord();
}
// Remove from list of all sections if this is the root declaration.
bool isRootDeclaration = IsRootDeclaration(configKey, false);
if (_sectionFactories != null && isRootDeclaration) {
_sectionFactories.Remove(configKey);
}
// Remove from collection of factory records.
if (!IsLocationConfig && _factoryRecords != null && _factoryRecords.Contains(configKey)) {
sectionIsUsed = true;
_factoryRecords.Remove(configKey);
}
// Remove from collection of section records.
if (_sectionRecords != null && _sectionRecords.Contains(configKey)) {
sectionIsUsed = true;
_sectionRecords.Remove(configKey);
}
// Remove all location section records for this section in this file.
if (_locationSections != null) {
int i = 0;
while (i < _locationSections.Count) {
LocationSectionRecord locationSectionRecord = (LocationSectionRecord) _locationSections[i];
if (locationSectionRecord.ConfigKey != configKey) {
i++;
}
else {
sectionIsUsed = true;
_locationSections.RemoveAt(i);
}
}
}
if (sectionIsUsed) {
// Add to RemovedSections since we need to remove
// it from the file later.
RemovedSections.Add(configKey, configKey);
}
//
// Remove all references from configSource
// Note that we can't remove an item while enumerating it.
//
List<string> streamsToRemove = new List<string>();
foreach (StreamInfo streamInfo in _streamInfoUpdates.Values) {
if (streamInfo.SectionName == configKey) {
streamsToRemove.Add(streamInfo.StreamName);
}
}
foreach (string stream in streamsToRemove) {
_streamInfoUpdates.Remove(stream);
}
}