2 // System.Configuration.AppSettingsSection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Chris Toshok (toshok@ximian.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
31 using System.ComponentModel;
32 using System.Collections.Specialized;
36 namespace System.Configuration {
38 public sealed class AppSettingsSection : ConfigurationSection
40 private static ConfigurationPropertyCollection _properties;
41 private static readonly ConfigurationProperty _propFile;
42 private static readonly ConfigurationProperty _propSettings;
44 static AppSettingsSection ()
46 _propFile = new ConfigurationProperty ("file", typeof(string), "",
47 new StringConverter(), null, ConfigurationPropertyOptions.None);
48 _propSettings = new ConfigurationProperty ("", typeof(KeyValueConfigurationCollection), null,
49 null, null, ConfigurationPropertyOptions.IsDefaultCollection);
51 _properties = new ConfigurationPropertyCollection ();
53 _properties.Add (_propFile);
54 _properties.Add (_propSettings);
57 public AppSettingsSection ()
61 protected internal override bool IsModified ()
63 return Settings.IsModified ();
66 [MonoInternalNote ("file path? do we use a System.Configuration api for opening it? do we keep it open? do we open it writable?")]
67 protected internal override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
69 /* need to do this so we pick up the File attribute */
70 base.DeserializeElement (reader, serializeCollectionKey);
74 string filePath = File;
75 if (!Path.IsPathRooted (filePath))
76 filePath = Path.Combine (Path.GetDirectoryName (Configuration.FilePath), filePath);
78 Stream s = System.IO.File.OpenRead (filePath);
79 XmlReader subreader = new ConfigXmlTextReader (s, filePath);
80 base.DeserializeElement (subreader, serializeCollectionKey);
84 // nada, we just ignore a missing/unreadble file
89 protected internal override void Reset (ConfigurationElement parentSection)
91 AppSettingsSection psec = parentSection as AppSettingsSection;
93 Settings.Reset (psec.Settings);
97 protected internal override string SerializeSection (
98 ConfigurationElement parent, string name, ConfigurationSaveMode mode)
101 return base.SerializeSection (parent, name, mode);
104 throw new NotImplementedException ();
108 [ConfigurationProperty ("file", DefaultValue = "")]
110 get { return (string)base [_propFile]; }
111 set { base [_propFile] = value; }
114 [ConfigurationProperty ("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
115 public KeyValueConfigurationCollection Settings {
116 get { return (KeyValueConfigurationCollection) base [_propSettings]; }
119 protected internal override ConfigurationPropertyCollection Properties {
125 protected internal override object GetRuntimeObject ()
127 KeyValueInternalCollection col = new KeyValueInternalCollection ();
129 foreach (string key in Settings.AllKeys) {
130 KeyValueConfigurationElement ele = Settings[key];
131 col.Add (ele.Key, ele.Value);
134 if (!ConfigurationManager.ConfigurationSystem.SupportsUserConfig)