AppSettingsSection
Provides configuration system support for the appSettings configuration section. This class cannot be inherited.
using System.Collections.Specialized;
using System.IO;
using System.Xml;
namespace System.Configuration
{
public sealed class AppSettingsSection : ConfigurationSection
{
private static volatile ConfigurationPropertyCollection s_properties;
private static volatile ConfigurationProperty s_propAppSettings;
private static volatile ConfigurationProperty s_propFile;
private KeyValueInternalCollection _keyValueCollection;
protected internal override ConfigurationPropertyCollection Properties => EnsureStaticPropertyBag();
internal NameValueCollection InternalSettings => _keyValueCollection ?? (_keyValueCollection = new KeyValueInternalCollection(this));
[ConfigurationProperty("", IsDefaultCollection = true)]
public KeyValueConfigurationCollection Settings {
get {
return (KeyValueConfigurationCollection)base[s_propAppSettings];
}
}
[ConfigurationProperty("file", DefaultValue = "")]
public string File {
get {
return ((string)base[s_propFile]) ?? string.Empty;
}
set {
base[s_propFile] = value;
}
}
public AppSettingsSection()
{
EnsureStaticPropertyBag();
}
private static ConfigurationPropertyCollection EnsureStaticPropertyBag()
{
if (s_properties != null)
return s_properties;
ConfigurationProperty property = new ConfigurationProperty(null, typeof(KeyValueConfigurationCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
ConfigurationProperty property2 = new ConfigurationProperty("file", typeof(string), string.Empty, ConfigurationPropertyOptions.None);
ConfigurationPropertyCollection obj = new ConfigurationPropertyCollection {
property,
property2
};
s_propAppSettings = property;
s_propFile = property2;
s_properties = obj;
return s_properties;
}
protected internal override object GetRuntimeObject()
{
SetReadOnly();
return InternalSettings;
}
protected internal override void Reset(ConfigurationElement parentSection)
{
_keyValueCollection = null;
base.Reset(parentSection);
if (!string.IsNullOrEmpty((string)base[s_propFile]))
SetPropertyValue(s_propFile, null, true);
}
protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
{
string name = reader.Name;
base.DeserializeElement(reader, serializeCollectionKey);
string file = File;
if (file != null && file.Length > 0) {
string source = base.ElementInformation.Source;
string text = string.IsNullOrEmpty(source) ? File : Path.Combine(Path.GetDirectoryName(source), File);
if (System.IO.File.Exists(text)) {
int lineNumber = default(int);
string rawXml = default(string);
using (Stream stream = new FileStream(text, FileMode.Open, FileAccess.Read, FileShare.Read))
using (XmlUtil xmlUtil = new XmlUtil(stream, text, true)) {
if (xmlUtil.Reader.Name != name)
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_name_value_file_section_file_invalid_root, name), xmlUtil);
lineNumber = xmlUtil.Reader.LineNumber;
rawXml = xmlUtil.CopySection();
while (!xmlUtil.Reader.EOF) {
if (xmlUtil.Reader.NodeType != XmlNodeType.Comment)
throw new ConfigurationErrorsException(System.SR.Config_source_file_format, xmlUtil);
xmlUtil.Reader.Read();
}
}
using (ConfigXmlReader configXmlReader = new ConfigXmlReader(rawXml, text, lineNumber)) {
configXmlReader.Read();
if (configXmlReader.MoveToNextAttribute())
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_base_unrecognized_attribute, configXmlReader.Name), (XmlReader)configXmlReader);
configXmlReader.MoveToElement();
base.DeserializeElement(configXmlReader, serializeCollectionKey);
}
}
}
}
}
}